M01: Introduction to Operating Systems
TU1: Installing, configuring and exploiting a computer system
ASIX1
Practical Exercise 10b: Power management,  stdin, stdout, stderr, redirection and pipes. 15-02-18

Practical Exercise 10b: Power management, sdtin, stdout, stderr, redirection and pipes 

GENERAL CONDITIONS
1- Deadline:
25-2-2018.
2- Send your report as a PDF file attached to an e-mail with the following specifications:
     a) E-mail address:
cf(at)collados.org or jordi.binefa(at)fje.edu depending who is your teacher
     b) File Name:

        b.1)
ASIX1 (Catalan): asix1_surname_name_m01tu01pr10b.pdf
        b2.) DAW1 (English): daw1_surname_name_m01tu01pr10b.pdf     
     c)
Subject:
        
c.1) ASIX1 (Catalan): asix1_surname_name_m01tu01pr10b
         c.2)
DAW1 (English): daw1_surname_name_m01tu01pr10b
3- Make this report individually.
4- Left, right, top and bottom margins: 2cm.
5- Character format: a) Font: Arial, b) Size: 10, c) Questions typeface: Bold, d) Answers typeface: Regular

1- Shutting down and restarting the system

To shut down a running Debian GNU/Linux system, you must not reboot with the reset switch on the front or back of your computer, or just turn off the computer. Debian GNU/Linux should be shut down in a controlled manner, otherwise files might get lost and/or disk damage might occur. In order to shut down your system in a controled manner, you can select one of the following options:

a)
If you run a desktop environment, there is usually an option to log out available from the application menu that allows you to shutdown (or reboot) the system.

b)
You can press the key combination Ctrl+Alt+Del.

c)
Another option is to log in as root and type the command systemctl poweroff to shut down and poweroff the system. If you run this command,  all remaining file systems are unmounted, all remaining swap devices are disabled, detached all remaining storage devices are detached,  all remaining processes are killed, cpu is halted and finally, the system is powered off, in other words, the power supply is disconnected.

d)
You can also run the command systemctl halt to shut down and halt the system. If you run the command, all remaining file systems are unmounted, all remaining swap devices are disabled, detached all remaining storage devices are detached,  all remaining processes are killed, and your cpu is halted, in other words, your cpu is stopped, doing nothing. In halt mode CPU is idle and the power consumption is reduced and its clock signal is stopped.

e)
Or if you prefer to shut down the system after a delay, without powering off the machine, you can run: shutdown --halt +m where +m is the delay in minuts. For instance shutdown --halt +10 halts the system in 10 minuts,

e) But if you want to power off your system after a delay, you should run shutdown --poweroff +m where +m is the delay in minuts.

e)
Finally, you can run the  systemctl reboot to reboot the system-that is, shutting down and booting the system. If you want to reboot your system after a delay then., you shoud run shutdown --reboot +m where +m is the delay in minuts.

2- Suspending and hibernating the system


a) To suspend the system, type the following at a shell prompt as root :

systemctl  suspend

This command saves the system state in RAM and with the exception of the RAM module, powers off most of the devices in the machine. When you turn the machine back on, the system then restores its state from RAM without having to boot again. Because the system state is saved in RAM and not on the hard disk, restoring the system from suspend mode is significantly faster than restoring it from hibernation, but as a consequence, a suspended system state is also vulnerable to power interruptions.

b) To hibernate the system, type the following at a shell prompt as root:

systemctl  hibernate

This command saves the system state on the hard disk drive and powers off the machine. When you turn the machine back on, the system then restores its state from the saved data without having to boot again. Because the system state is saved on the hard disk and not in RAM, the machine does not have to maintain electrical power to the RAM module, but as a consequence, restoring the system from hibernation is significantly slower than restoring it from suspend mode.

3- Standard input, standart output and standard error files

a) Standard output (stdout):Is a file where  a process can send information, such a text. An standard output file can be a device (a printer for instance), an ordinary file or the screen. By default, the standard output file is the screen.
b) Standard input (stdin): is a file where a process gets information from. An standard input file can be an ordinary file or the keyboard. By default, the standard input file is the keyboard.
c) Standard error (stderr): is a place where a process can send error messages. An standard input file can be a device (a printer for instance), an ordinary file or the screen. By default, the standard output file is the screen.

When you run a  program, Linux assigns an standard input, standard output and standard error files for the process. Each one is identified by a number called file descriptor. The standard input file is identified by the number 0,  standard output fileis identified by the number 1 and standard error file is identified by the number 2. When a program sends output data, it sends data to the file identified by the file descriptor 0. 

4- Redirection

By default the standard input is the keyboard and the standard output is the screen.  Redirection encompasses the various ways you can cause the system to alter where stdin of a command comes from and where the stdout goes to. You can redirect the process standard output to an ordinary file or device, and you can redirect data coming from a device or file to the process standard input.

4.1- How to redirect the standard output (stdout) to an ordinary file or device. Output redirection

The most common use of Redirection is to redirect the output (that normally goes to the terminal display, that is, the screen) from a command to a file instead. This is known as Output Redirection. This is generally used when you get a lot of output when you execute your program. Often you see that screens scroll past very rapidly. You could get all the output in a file and then even transfer that file elsewhere or mail it to someone.

In order to redirect the standard output to an ordinary file or device you should use the the following symbols:


a)
The redirect output symbol  > : Instructs the system to redirect the output of a process (a command) to the specified file or device instead of the screen. If you redirect output to a file, and the file does not exist, the system creates the file. If you redirect output to a file, and the file exists, the system will overwrite the file and destroy  its old contents.

Example:

ip  a  >  ipinfo.txt

When you run this command, ip  redirect output data to a new file called ipinfo.txt. You can use any kind of file name and extension. Extensions are not mandatory.

b) The append output symbol  >> : Instructs the system to redirect and add the output of a process (a command) to the specified file or device instead of the screen. If you redirect output to a file, and the file does not exist, the system creates the file. If you redirect output to a file, and the file exists, the system will add the new contents  to the end of the file, leaving existing information intact.

Example:

ip  route  >  ipinfo.txt

When you run this command, ip  redirect output data to an existing file called ipinfo.txt.
Data output is added to end of the existing file.


4.2- How to redirect the standard input (stdin)  from ordinary files or device. Input redirection

Input redirection is not as popular as Output Redirection. Since most of the times you would expect the input to be typed at the keyboard. But when it is used effectively, Input Redirection can be of great use. The general use of Input Redirection is when you have some kind of file, which you have ready and now you would like to use some command on that file (but even in this case, most of the commands can read from a file and it makes sometimes redirection of standard input is not too much useful and popular  at bash level).

In order to redirect the standard input to come from ordinary files or device instead of the keyboard  you should use the the following symbol: < .

Example:

mysql -u root -p < showtable.sql

where showtable.sql is a text file where you have written some SQL sentences. The mysql client will read SQL commands coming from showtable.sql instead of reading them coming from the keyboard. You can write a lot of SQL commands your showtable.sql file.


4.3- How to redirect the standard error (stderr)  to an ordinary file or device. Error redirectiom

Standard error writes the errors generated by a program that has failed at some point in its execution. Like standard output, the default destination for the standard error file is the screen.
A lot of commands show error messages on the screen. Sometimes errors messages from the screen is a useful option. But sometimes is more useful to redirect error messages to an ordinary file text where these messages can be easily read with the help of a  text editor and additionally, the messages are not lost when the system display new data to the screen.

In order to redirect the standard error  to an ordinary file or device you should use the the following symbols: 2> (to create a new file) or 2>> (to append data to an existing file).

Example 1:

find / -name *.conf  2> err.log

When you run this command, find redirect any error message to a new file called err.log. You can use any kind of file name and extension. Extensions are no mandatory

Example 2:

  ls  -R  /etc  2>> err.log

When you run this command, ls  redirect any error message to an existing file called err.log. Error messages are added to end of the existing file.


Example 3:

find / -name *.conf  2> /dev/null

When you run this command, find redirect any error message to /dev/null. This device is typically used for disposing  of unwanted output data of a process and therefore, data sent to /dev/null shall be discarded.

5- Pipes

Pipes as the name suggests is a sort of hollow tube (a pipe) where you can put data into one end and get it out of the other end.  Using pipes you can connect two programs. Using pipes you can make the output of a particular command to act as the input for another command.

Be careful, sometime output data from the first command is not acceptable for the second command  and you will get an error message or press ctrl-c to quit the execution of the commnad.


The pipe character is |

You can

Examples:

ps aux  | grep firefox   --> The output data of ps aux is redirected as an  input data to grep. Output data of grep is sent to the screen.

ps aux  | grep firefox   --> The output data of ps aux is redirected as an  input data to grep. Output data of grep is sent to an ordinary file called psffox.

Pipes let you pass the output of one command to the input of another command. You can carry on this chain as long as you want (you can use pipes between 5,6,7..commands..how many ever you want) and you can get extremely customized outputs.

 Example:

ls -ls | grep Public | tr -s " " | cut -c 2- | cut -d " " -f4

When you run this command, you get the username of the  Public directory owner. Command tr -s " " remove repeated white spaces. Command cut -c 2-  cuts the original string and shows a new string starting from the second the character to the end. Command
cut -d " " -f4  cuts the original string in smaller string using white space " " as a delimiter character.

PRACTICAL EXERCISE

1- Halt your system. What is happening?. Is running?
2- Power off your system.
3- Boot your system. Power off your system. What is the difference between halt and poweroff?.

4- Boot your system. Shut down and power off your system in 1 minute.
5- Boot your system. Shut down and reboot  your system in 2 minutes.
6- Run echo "Now is the winter of our descontent," > r3.txt. What happens?. What is the purpouse of the command echo?. Why a new file called r3.txt is created on your system?.
7-
Run echo "made glorious summer by this sun of  York" > r3.txt. What happens?. Why the old contents of r3.txt no longer exist?.
8- Run
echo "Now is the winter of our descontent," > r3.txt and afterwards echo "made glorious summer by this sun of  York" >> r3.txt. What happens?. Why?.
9- What famous play of Shakespeare starts with these two sentences?.
10- List the contents of your personal folder and redirect the output to an ordinary file called lstpers.txt.
11-
List the contents of /etc and  add  the output to lstpers.txt.
12- Redirect the contents of showing /etc/passwd with cat to a file called usrlst.txt.
13- Run the following command: ip -4 a | grep "inet" | grep -v "127.0.0.1" | cut -d " " -f6 | cut -d "/" -f1 . What is the result of running this command?
14- Run the following command: ip route | grep "default" | cut -d " " -f3 . What is the result of running this command?
15- Run the following command: cat /etc/resolv.conf | grep nameserver | cut -d " " -f2 .What is the result of running this command?
16-
Create a file called network.conf with  the  system ip address, the router ip address and the dns servers ip addresses. In order to create this file, you must run the commands that were studied in the previous question  and work with the output redirection symbols.
17- Run a command using pipes to show the permissions of the Public directory.
18-
Run a command using pipes to show a list of users defined in your system and redirect the result to a file called lstur.txt in your personal folder.
19-
Run a command using pipes, show the state of the bash process (only bash).
20- Find any file with the extension .sh installed on /usr  and redirect the output to a file in your personal folder called specialUsrFiles.lst.
21- Find any file with the extension .gz installed on /usr  and redirect the output to specialUsrFiles.lst. New contents will be added to the end of the file.
22- List recursively content of /etc as a normal user redirecting the error messages to a file called errors in your personal folder. Show errors.
23- Create a folder called log in your persona directory. Change to log. Copy  any file in /var/log into your log directory. Redirect and add any error message to the file errors created in the previous question. Show errors.