1.19. Process Control

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.

1.19.1. External Commands

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 uses -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
	    

1.19.2. Special Key Combinations

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.

1.19.3. Internal Shell Commands and Symbols

jobs lists the processes running under the current shell, but using the shell's job IDs instead of the system's process IDs.

Caution

Shell jobs are ordinary processes running on the local system and should not be confused with cluster and grid jobs, which are managed by networked schedulers.
shell-prompt: jobs
	    

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.

1.19.4. Self-test

  1. What is a process?
  2. What is the difference between a foreground process and a background process?
  3. How many foreground processes can be running at once under a single shell process? Why?
  4. How many background processes can be running at once under a single shell process? Why?
  5. Show the simplest Unix command that will accomplish each of the following:
    1. List all processes currently running.
    2. List processes owned by you.
    3. Kill the process with ID 7243.
    4. Kill all processes running the program netsim.
    5. Kill the process with ID 7243 after the first attempt failed.
  6. How do you perform each of the following tasks?
    1. Kill the current foreground process.
    2. Suspend the current foreground process.
    3. Resume a suspended process in the foreground.
    4. Resume a suspended process in the background.
    5. Start a new process, placing it in the background immediately.
    6. Suspend terminal output for a process without suspending the process itself.
    7. Resume suspended terminal output.
    8. List the currently running jobs as seen by the shell.
    9. Return job #2 to the foreground.
    10. Run the program netsim at a reduced priority so that other processes will respond faster.
    11. Run the program netsim and report the CPU time used when it finishes.