Unix systems provide many tools for managing and monitoring processes that are already running.
Note that these tools apply to local Unix processes only. On distributed systems such as clusters and grids, job management is done using networked schedulers such as HTCondor, Grid Engine, or PBS.
It is possible to have multiple processes running under the same shell session. Such processes are considered either foreground processes or background processes. The foreground process is simply the process that receives the keyboard input. There can be no more than one foreground process under a given shell session, for obvious reasons.
Note that all processes, both foreground and background, can send output to the terminal at the same time, however. It is up to the user to ensure that output is managed properly and not intermixed.
There are three types of tools for process management, described in the following subsections.
Unix systems provide a variety of external commands that monitor or manipulate processes based on their process ID (PID). A few of the most common commands are described below.
ps lists the currently running processes.
shell-prompt: ps [-a] # BSD shell-prompt: ps [-e] # SYSV
ps is one of the rare commands whose options
vary across different Unix systems. There are only two standards
to which it may conform, however. The BSD version uses
-a to indicate that all processes (not just your
own) should be shown. System 5 (SYSV) ps
-e for the same purpose. Run
man ps on your system to determine which
flags should be used.
kill sends a signal to a process (which may kill the process, but could serve other purposes).
shell-prompt: kill [-#] pid
The pid (process ID) is determined from the output of ps.
The signal number is an integer
value following a -, such as
-9. If not provided, the default
signal sent is the TERM (terminate) signal.
Some processes ignore the TERM signal. Such processes can be force killed using the KILL (9) signal.
shell-prompt: kill -9 2342
Run man signal to learn about all the signals that can be issued with kill.
shell-prompt: ps PID TT STAT TIME COMMAND 41167 0 Is 0:00.25 tcsh 78555 0 S+ 0:01.98 ape unix.dbk shell-prompt: kill 78555
The killall command will kill all processes running the program named as the argument. This eliminates the need to find the PID first, and is more convenient for killing multiple processes running the same program.
shell-prompt: killall fdtd
Ctrl+c sends a terminate signal to the current foreground process. This usually kills the process immediately, although it is possible that some processes will ignore the signal.
Ctrl+z sends a suspend signal to the current foreground process. The process remains in memory, but does not execute further until it receives a resume signal (usually sent by running fg).
Ctrl+s suspends output to the terminal. This does not technically control the process directly, but has the effect of blocking any processes that are sending output, since they will stop running until the terminal begins accepting output again.
Ctrl+q resumes output to the terminal if it has been suspended.
jobs lists the processes running under the current shell, but using the shell's job IDs instead of the system's process IDs.
fg brings a background job into the foreground.
shell-prompt: fg [%job-id]
There cannot be another job already running in the foreground. If no job ID is provided, and multiple background jobs are running, the shell will choose which background job to bring to the foreground. A job ID should always be provided if more than one background job is running.
bg resumes a job suspended by Ctrl+z in the background.
shell-prompt: prog Ctrl+z shell-prompt: bg shell-prompt:
An & at the end of any command causes the command to be immediately placed in the background. It can be brought to the foreground using fg at any time.
shell-prompt: command &
nice runs a process at a lower than normal priority.
shell-prompt: nice command
If (and only if) other processes in the system are competing for CPU time, they will get a bigger share than processes run under nice.
time runs a command under the scrutiny of the time command, which keeps track of the process's resource usage.
shell-prompt: time command
There are both internal and external implementations of the time command. Run which time to determine which one your shell is configured to use.
nohup allows you to run a command that will continue after you log out. Naturally, all input and output must be redirected away from the terminal in order for this to work.
Bourne shell and compatible:
shell-prompt: nohup ./myprogram < inputfile > outputfile 2>&1
C shell and compatible:
shell-prompt: nohup ./myprogram < inputfile >& outputfile
This is often useful for long-running commands and where network connections are not reliable.
There are also free add-on programs such as GNU screen that allow a session to be resumed if it's disrupted for any reason.