Last modified on 4 October 2012, at 21:20

Ict-innovation/LPI/103.5

103.5 Create, Monitor and Kill ProcessesEdit

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 JobsEdit

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:

$ xclock

xclock running in forground, shell prompt lost

[1]+ Stopped xclock xclock received ^Z signal

$ bg shell prompt recovered, issue the bg command

[1]+ xclock & xclock is running in the background

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.

$ xclock&

[1] 6213

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.

Output for jobs
[1]- Stopped xclock

[2] Running xman &

[3]+ 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.

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:

$ nohup bigbang &


Viewing Running ProcessesEdit

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

`-xscreensaver(1098)

bash(1046)---xinit(1085)-+-X(1086)

`-xfwm(1094)-+-xfce(1100)---xterm(1111)---bash(1113)-+-pstree(1180)

-soffice.bin(1139)---soffice.bin(1152)-+

-soffice.bin(1153)

|-soffice.bin(1154) |-soffice.bin(1155) |-soffice.bin(1156) `-soffice.bin(1157) `-xclock(1138)

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!

ps accommodates UNIX-style and BSD-style arguments
usage: ps -[Unix98 options]

ps [BSD-style options]

ps --[GNU-style long options]

ps --help for a command summary


Summary of options
-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 ProcessesEdit

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”.

Most Common Signals
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

Syntax

killall SIGNAL process_NAME



Used files, terms and utilities:* &

  • bg
  • fg
  • jobs
  • kill
  • nohup
  • ps
  • top
  • free
  • uptime
  • killall


Previous Chapter | Next Chapter