You get a bonus - 1 coin for daily activity. Now you have 1 coin

14 - Processes, tasks. Commands fg, bg, jobs, top, nohup

Lecture



In this lecture, we will finish the topic of processes and signals in Linux and move on to the topic of mounting file systems. In the last lecture, we repeatedly used the & ( ampersand ) sign at the end of the command ( sleep 100 & ). The ampersand sign at the end of the command means that the process to be started will need to be transferred to the background. If you run the com *** in sleep 100 without the & sign, then we will not receive an invitation on the command line until the sleep process is completed.

Commands launched from the console using an ampersand work in the background and are called jobs . We can say that tasks are processes bound to the command interpreter. Such tasks, in addition to the traditional PID, also have their numbering starting with one. View running tasks interpreter, you can com *** th jobs . The example below shows a situation when there are two tasks and the execution of one of them is stopped.

one
2
3
$ jobs
[1] + Stopped top
[2] - Running sleep 100 &

Type in the console com *** at top . This com *** a shows real-time existing processes, but not all, but only the part that fits on the screen. We will return to this team a little later. For now, you may notice that top does not return control to the command interpreter. You can either exit the program (by pressing q) or stop the process with the key combination ctrl + z (do not confuse the combination ctrl + c - end of the process and ctrl + z - stop the process). We stopped the top process in the example above. In order to resume the work of this process (task) there are two commands: fg and bg , abbreviations from the English words foreground (foreground) and background (background). The syntax is simple: fg task number. Kom *** and fg , works not only with stopped tasks, but also with tasks in general. In our example, a comma *** and fg 2 will bring the sleep process to the fore and the command interpreter's invitation will become unavailable (as if we started a com *** in sleep without the & character). Com *** fg 2 will resume the process of top and bring it to the fore. A com *** and fg without a parameter will resume the work of the last process stopped by the ctrl + z combination, and if there are no such processes, it will bring to the fore the last task (the task with a large sequence number). The task that will be restored (displayed) com *** th fg without a parameter is marked with + in the output of the jobs command. Com *** and bg is designed to restore the work of stopped processes (tasks) in the background.

Also note that tasks have their own numbering for each terminal (console). If you register in another console and run the process in the background, then the task number will start with one. Also, you will not find help for com *** am fg and bg ( man fg, man bg ). Because these commands are part of bash . And you will find a mention of them in man bash .

Any processes running from the shell are child processes for it. And the PPID of such processes will be equal to the PID of the corresponding command interpreter. If you exit the command interpreter, all processes started from it (including those that are in the background) will be terminated. Register in two consoles and run several processes in the second one, go to the first one and execute the *** command in ps al :

one
2
3
four
five
6
7
eight
9
$ ps al
F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND
0 1000 13180 1053 20 0 6292 3552 wait Ss pts / 0 0:00 bash
0 1000 16442 1054 20 0 6292 3560 wait Ss pts / 1 0:00 bash
0 1000 16460 16442 20 0 2952 628 signal T pts / 1 0:00 sleep 200
0 1000 16461 16442 20 0 18416 4392 signal T pts / 1 0:00 mocp
0 1000 16469 16442 20 0 2952 628 hrtime S pts / 1 0:00 sleep 1000
0 1000 16470 16442 20 0 2468 1208 poll_s S + pts / 1 0:00 top
0 1000 16473 13180 20 0 2424 828 - R + pts / 0 0:00 ps al

In the example, we run two bash command interpreters with a PID of 13180 and 16442. Then follow the 4th processes that were launched from the second command interpreter - they can be identified by a PPID of 16442. Now exit (comma and exit ) from the second interpreter and execute the *** again in ps al in the first interpreter:

one
2
3
four
$ ps al
F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND
0 1000 13180 1053 20 0 6292 3556 wait Ss pts / 0 0:00 bash
0 1000 16679 13180 20 0 2424 824 - R + pts / 0 0:00 ps al

As you can see, not only the process with PID 16442, but all descendant processes ( PPID 16442) was completed. Another confirmation that a child process cannot exist without a parent process.

But what to do if you need to start the process and exit the console so that the process continues to work in the system? Only one way out is to pass the child process to another process that will be its parent. This feature is implemented in the nohup command. This com *** a allows you to start processes that will be disconnected from the terminal if the terminal is closed. We are conducting an experiment. We start any com *** y (for example, ping ) through nohup in one console and without leaving it we are looking at the processes:

one
$ nohup ping 127.0.0.1 &
one
2
3
four
five
6
$ ps alx
F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND
4 0 17326 1 20 0 7908 3208 wait Ss tty2 0:00 / bin / login -
4 1000 17427 17326 20 0 6304 3560 wait S tty2 0:00 -bash
4 1000 17450 17427 20 0 1848 556 wait_f S tty2 0:00 ping 127.0.0.1
0 1000 17517 13180 20 0 2424 848 - R + pts / 0 0:00 ps alx

We see that now the ping process has a PPID equal to 17427 (that is, it is a descendant of the bash command interpreter) and PID 17450. Now we will exit the bash command interpreter with PID 17427 and look again at the processes:

one
2
3
$ ps alx
F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND
4 1000 17450 1 20 0 1848 556 poll_s S? 0:00 ping 127.0.0.1

As we can see, despite the fact that we left the command interpreter, the process with PID 17450 remained in the system and took as its parent, the process with PID equal to 1, that is, the init process. Process 17450 will exist as long as the init process exists, or until we finish its work using the kill command.

A com *** and ps shows very detailed information about the processes, but it is almost useless if you need to track the process in real time, see what resources and in what quantity the process takes. For this there is another com *** a - top . Com *** and top works interactively and in real time displays the work of processes. To run you just need to dial a com *** at - top . Exit - the q key. Processes are displayed not all, but only the upper part of the process table sorted by any criterion (column). The information that is displayed by the com *** and top can be divided into two parts: the top one with general information about the system and resources, and the bottom process table. At the top you can see information about how many days the system is running, how much RAM is occupied, how many processes are running and other information:

one
2
3
four
five
top - 17:09:05 up 2 days, 1:48, 6 users, load average: 0.08, 0.05, 0.04
Tasks: 189 total, 1 running, 188 sleeping, 0 stopped, 0 zombie
Cpu (s): 4.7% us, 1.5% sy, 0.0% ni, 93.8% id, 0.0% wa, 0.0% hi, 0.0% si, 0.0% st
Mem: 1024756k total, 785880k used, 238876k free, 16372k buffers
Swap: 1140544k total, 287012k used, 853532k free, 365048k cached

By default, processes are sorted by processor load level. If you need to change the sorting order, you need to press the key combination Shift + o or Shift + f . After pressing this key combination, a window will be displayed in which you can select the column by which you want to sort the table. For example, to sort a table by the amount of memory used, you need to press the Shift + o combination, then the n key and press Enter . To change the sorting order (descending / increasing), it is necessary to press the Shift + r combination.

To get a brief information about the program top you need to press the h key.

To display the processes of a specific user, in the running top program, press the u key and then type the account name and press Enter . To display all the processes again, press u and without typing anything, press Enter .

It is very convenient to use top to remove the processes that load the system. Such processes (loading the processor at 100%) will by default be at the top of the table. We look at the PID process, press the k key, write the PID and press Enter . Then we write the number of the signal 9 and again Enter .

The top command has many more features, see the help for details. This will end the topic of processes and signals. The next lecture will be devoted to mounting file systems.

See also


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

LINUX operating system

Terms: LINUX operating system