Ict-innovation/LPI/103.5

= 103.5 Create, Monitor and Kill Processes = Candidates should be able to perform basic process management.

Key Knowledge Areas


 * Run jobs in the foreground and background.
 * Signal a program to continue running after logout.
 * Monitor active processes.
 * Select and sort processes for display.
 * Send signals to processes.

When the shell runs a command, it normally waits and will not prompt for further input until that command has completed. The command is said to run in the foreground.

When a program is running in the foreground it is possible to recover the shell prompt but only by interrupting the program for while. The interruption signal is Ctrl Z.

Starting and Stopping Jobs
A process started from a shell is also called a ''job. ''Once the job receives the ^Z signal it is stopped and the shell prompt is recovered. To restart the program in the background simply type: bg.

Example:

Notice the [1]+ symbol above. The integer is the process' job number, which it can be referred to as.

The '+' sign indicates the last modified process. A '-' sign would indicate the second last modified process. One can start a process in the background by appending a & to the command.

The numbers reported here are the job numbers (in square brackets), and the process ID.

Listing jobs

The jobs utility lists all running processes started from the current shell. The job number, the job's state (running/stopped), as well as the two last modified processes, will be listed.

The job number

One can conveniently stop and start a selection of jobs using the job number. This is achieved with the fg command.

Calling job 2 to the foreground and killing job 1 

fg 2 or fg %2 or fg %?xma ! kill –9 %1

Avoiding HUP with nohup

There is a program called nohup which acts as a parent process independently from the user’s session. When a user logs off, the system sends a HUP signal to all processes owned by that process group. For example, to avoid this HUP signal a script called bigbang which attempts to calculate the age of the Universe should be started like this:

Viewing Running Processes
Processes have a unique Process ID the PID. This number can be used to modify a process' priority or to stop it. A process is any running executable. If process_2 has been spawned by process_1, it is called a child process. The spawning process_1 is called the parent process.

The pstree command gives a good illustration of parent and child process hierarchy.

Figure 103.5.1: Part of the pstree output

In the above figure all the process' PIDs are shown; these are clearly incremental. The most common used options are -p to display PIDs and -h to highlight a users processes only.

A more direct way to determine which processes are running is to use ps. Most users learn a favourite combination of options which work for most situations.

Here are three such options:

ps ux all processes run by the user

ps Tprocesses run under the current terminal by the user

ps auxall processes on the system

It is recommended you read the ps manpage and choose your own best options!

Sending Signals To Processes
The kill command can be used to send signals to processes. There are 63 signals available. The default signal terminates a process and is called SIGTERM with value 15.

kill

Syntax

kill SIGNAL process_PID

Unless you are root, you can only send signals to processes that you own.

Every process can choose whether or not to catch a signal except for the SIGKILL which is dealt with by the kernel. Most daemons use SIGHUP to mean “re-read configuration file”.

One can also stop processes without knowing the process' PID using killall.

killall

Syntax

Used files, terms and utilities:* &
 * bg
 * fg
 * jobs
 * kill
 * nohup
 * ps
 * top
 * free
 * uptime
 * killall

Previous Chapter | Next Chapter