Job control (Unix)
When using Unix or related operating systems via a terminal, a user will initially only have a single process running, their login shell. Most tasks (directory listing, editing files, etc.) can easily be accomplished by letting the program take control of the terminal and returning control to the shell when the program exits; however, sometimes the user will wish to carry out a task in the background while using the terminal for another purpose. Job control is a facility developed to make this possible, by allowing the user to start programs in the background, send programs into the background, bring background processes into the foreground, and start and stop running programs. Processes under the influence of a job control facility are referred to as jobs.
Job control was first implemented in the C shell by Jim Kulp, then at IIASA in Austria, making use of features of the 4.1BSD kernel. The Korn shell, developed at Bell Labs, adopted it and it was later incorporated into the SVR4 version of the Bourne shell, and exists in most modern Unix shells.
Typically, the shell keeps a list of jobs in a job table. A job consists of all the members of a pipeline; thus all the processes constituting the job will be in the same process group. The
jobs command will list the background jobs existing in the job table, along with their job number and job state (stopped or running). The
disown command can be used to remove jobs from the job table, converting them from jobs into daemons so that they continue executing when the user logs out.
A task running in the foreground can be stopped by typing the suspend character (Ctrl-Z). This sends the SIGTSTP signal to the process group. By default, SIGTSTP causes processes receiving it to stop, and control is returned to the shell. However, a process can register a signal handler for or ignore SIGTSTP. A process can also be paused with the SIGSTOP signal, which cannot be caught or ignored.
A stopped job can be resumed as a background job with the bg builtin, or as the foreground job with fg. In either case, the shell redirects I/O appropriately, and sends the SIGCONT signal to the process, which causes the operating system to resume its execution. In Bash, a program can be started as a background task by appending an ampersand (&) to the command line; its output is directed to the terminal (potentially interleaved with other programs' output), but it cannot read from the terminal input.
A background process that attempts to read from or write to its controlling terminal is sent a SIGTTIN (for input) or SIGTTOU (for output) signal. These signals stop the process by default, but they may also be handled in other ways. Shells often override the default stop action of SIGTTOU so that background jobs deliver their output to the controlling terminal by default.
In Bash-compatible shells, the
kill builtin (not
/bin/kill) can signal jobs by job ID as well as by process ID: jobs specified by a job ID should be killed by prefixing "
%". Kill can send any signal to a job, however if the intent is to rid the system of the processes the signals SIGKILL and SIGTERM (the default), are probably the most applicable. A task running in the foreground can be permanently suspended by typing the kill character (Ctrl-C).
- Marshall Kirk McKusick and George V. Neville-Neil (2004-08-02). "FreeBSD Process Management: Process Groups and Sessions". The Design and Implementation of the FreeBSD Operating System. Addison Wesley. ISBN 0-201-70245-2.
- "Job Control", Bash Reference Manual