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.
xclock running in forground, shell prompt lost
+ Stopped xclock xclock received ^Z signal
$ bg shell prompt recovered, issue the bg command
+ xclock & xclock is running in the background
Notice the + 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.
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.
|- Stopped xclock
 Running xman &
+ Stopped xload
The job number
One can conveniently stop and start a selection of jobs using the job number. This is achieved with the fg command.
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:
$ nohup bigbang &
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.
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!
|usage: ps -[Unix98 options]
ps [BSD-style options]
ps --[GNU-style long options]
ps --help for a command summary
|-a show all processes for the current user linked to a tty (except the session leader)
-e or -A show all processes
-f gives the PPID (Parent Process ID) and the STIME (Start Time)
-l is similar to -f and displays a long list
a show all processes linked to a tty, including other users
x show all processes without a controlling tty as well
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 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”.
|1 or SIGHUP hangup or disconnect the process
2 or SIGINT same as Ctrl+C interrupt
3 or SIGQUIT quit
9 or SIGKILL kill the process through a kernel call
15 or SIGTERM terminate a process 'nicely'. This is the DEFAULT signal.
One can also stop processes without knowing the process' PID using killall.
|killall SIGNAL process_NAME|
Used files, terms and utilities:* &