In
Unix
Unix (; trademarked as UNIX) is a family of multitasking, multiuser computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, and ot ...
and
Unix-like
A Unix-like (sometimes referred to as UN*X or *nix) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Unix-li ...
operating system
An operating system (OS) is system software that manages computer hardware, software resources, and provides common services for computer programs.
Time-sharing operating systems schedule tasks for efficient use of the system and may also in ...
s, job control refers to control of jobs by a
shell
Shell may refer to:
Architecture and design
* Shell (structure), a thin structure
** Concrete shell, a thin shell of concrete, usually with no interior columns or exterior buttresses
** Thin-shell structure
Science Biology
* Seashell, a hard ou ...
, especially interactively, where a "job" is a shell's representation for a
process group
In a POSIX-conformant operating system, a process group denotes a collection of one or more processes.
Among other things, a process group is used to control the distribution of a signal;
when a signal is directed to a process group, the signal i ...
. Basic job control features are the suspending, resuming, or terminating of all processes in the job/process group; more advanced features can be performed by sending
signals
In signal processing, a signal is a function that conveys information about a phenomenon. Any quantity that can vary over space or time can be used as a signal to share messages between observers. The ''IEEE Transactions on Signal Processing'' ...
to the job. Job control is of particular interest in Unix due to its
multiprocessing
Multiprocessing is the use of two or more central processing units (CPUs) within a single computer system. The term also refers to the ability of a system to support more than one processor or the ability to allocate tasks between them. There ar ...
, and should be distinguished from
job control generally, which is frequently applied to sequential execution (
batch processing
Computerized batch processing is a method of running software programs called jobs in batches automatically. While users are required to submit the jobs, no other interaction by the user is required to process the batch. Batches may automatically ...
).
Overview
When using
Unix
Unix (; trademarked as UNIX) is a family of multitasking, multiuser computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, and ot ...
or
Unix-like
A Unix-like (sometimes referred to as UN*X or *nix) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Unix-li ...
operating systems via a
terminal
Terminal may refer to:
Computing Hardware
* Terminal (electronics), a device for joining electrical circuits together
* Terminal (telecommunication), a device communicating over a line
* Computer terminal, a set of primary input and output devic ...
(or
terminal emulator
A terminal emulator, or terminal application, is a computer program that emulates a video terminal within some other display architecture. Though typically synonymous with a shell or text terminal, the term ''terminal'' covers all remote termin ...
), a user will initially only have a single process running, their
login
In computer security, logging in (or logging on, signing in, or signing on) is the process by which an individual gains access to a computer system by identifying and authenticating themselves. The user credentials are typically some form ...
shell
Shell may refer to:
Architecture and design
* Shell (structure), a thin structure
** Concrete shell, a thin shell of concrete, usually with no interior columns or exterior buttresses
** Thin-shell structure
Science Biology
* Seashell, a hard ou ...
. 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 – formally, by attaching to
standard input and standard output to the shell, which reads or writes from the terminal, and catching signals sent from the keyboard, like the termination signal resulting from pressing .
However, sometimes the user will wish to carry out a task while using the terminal for another purpose. A task that is running but is not receiving input from the terminal is said to be running "in the background", while the single task that is receiving input from the terminal is "in the foreground". Job control is a facility developed to make this possible, by allowing the user to start processes in the background, send already running processes into the background, bring
background process
A background process is a computer process that runs ''behind the scenes'' (i.e., in the background) and without user intervention. Typical tasks for these processes include logging, system monitoring, scheduling, and user notification. The backgr ...
es into the foreground, and suspend or terminate processes.
The concept of a ''job'' maps the (shell) concept of a single shell command to the (operating system) concept of the possibly many processes that the command entails. Multi-process tasks come about because processes may create additional child processes, and a single shell command may consist of a
pipeline
Pipeline may refer to:
Electronics, computers and computing
* Pipeline (computing), a chain of data-processing stages or a CPU optimization found on
** Instruction pipelining, a technique for implementing instruction-level parallelism within a s ...
of multiple communicating processes. For example, a command to select lines containing the text "title", sort these alphabetically, and display the result in a
pager
A pager (also known as a beeper or bleeper) is a wireless telecommunications device that receives and displays alphanumeric or voice messages. One-way pagers can only receive messages, while response pagers and two-way pagers can also acknow ...
.
grep title somefile.txt , sort , less
This creates at least three processes: one for , one for , and one for
. Job control allows the shell to control these related processes as one entity, and when a user issues the appropriate key combination (usually ), the entire group of processes gets suspended.
Jobs are managed by the operating system as a single
process group
In a POSIX-conformant operating system, a process group denotes a collection of one or more processes.
Among other things, a process group is used to control the distribution of a signal;
when a signal is directed to a process group, the signal i ...
, and the job is the shell's internal representation of such a group. This is defined in
POSIX
The Portable Operating System Interface (POSIX) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines both the system- and user-level application programming interf ...
as:
A job can be referred to by a
handle
A handle is a part of, or attachment to, an object that allows it to be grasped and manipulated by hand. The design of each type of handle involves substantial ergonomic issues, even where these are dealt with intuitively or by following tra ...
called the ''job control job ID'' or simply ', which is used by
shell builtin
In computing, a shell builtin is a command or a function, called from a shell, that is executed directly in the shell itself, instead of an external executable program which the shell would load and execute.
Shell builtins work significantly fast ...
s to refer to the job. Job IDs begin with the
%
character;
%n
identifies job ''n'', while
%%
identifies the current job. Other job IDs are specified by
POSIX
The Portable Operating System Interface (POSIX) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines both the system- and user-level application programming interf ...
. In informal usage the number may be referred to as the "job number" or "job ID", and Bash documentation refers to the (%-prefixed) job ID as the ''jobspec.''
Job control and job IDs are typically only used in interactive use, where they simplify referring to process groups; in scripting PGIDs are used instead, as they are more precise and robust, and indeed job control is disabled by default in bash scripts.
History
Job control was first implemented in the
C shell
The C shell (csh or the improved version, tcsh) is a Unix shell created by Bill Joy while he was a graduate student at University of California, Berkeley in the late 1970s. It has been widely distributed, beginning with the 2BSD release of the ...
by Jim Kulp, then at
IIASA
The International Institute for Applied Systems Analysis (IIASA) is an independent international research institute located in Laxenburg, near Vienna, in Austria. Through its research programs and initiatives, the institute conducts policy-or ...
in Austria, making use of features of the 4.1
BSD
The Berkeley Software Distribution or Berkeley Standard Distribution (BSD) is a discontinued operating system based on Research Unix, developed and distributed by the Computer Systems Research Group (CSRG) at the University of California, Berk ...
kernel.
The
KornShell
KornShell (ksh) is a Unix shell which was developed by David Korn at Bell Labs in the early 1980s and announced at USENIX on July 14, 1983. The initial development was based on Bourne shell source code. Other early contributors were Bell ...
, developed at Bell Labs, adopted it and it was later incorporated into the SVR4 version of the
Bourne shell
The Bourne shell (sh) is a Shell (computing), shell Command-line interface#Command-line interpreter, command-line interpreter for computer operating systems.
The Bourne shell was the default Unix shell, shell for Version 7 Unix. Unix-like syste ...
, and exists in most modern Unix shells.
Commands
The POSIX standard specifies two commands for resuming suspended jobs in the background and foreground, respectively and . These were modeled after the Korn shell job control commands.
[; .]
Implementation
Typically, the shell keeps a list of jobs in a job table. Recall that a job corresponds to a process group, which consists of all the members of a
pipeline
Pipeline may refer to:
Electronics, computers and computing
* Pipeline (computing), a chain of data-processing stages or a CPU optimization found on
** Instruction pipelining, a technique for implementing instruction-level parallelism within a s ...
and their descendants. The
jobs
command will list the background jobs existing in the job table, along with their job number and job state (stopped or running). When a
session ends when the user
logs out (exits the shell, which terminates the ''session leader'' process), the shell process sends
SIGHUP On POSIX-compliant platforms, SIGHUP ("signal hang up") is a signal sent to a process when its controlling terminal is closed. It was originally designed to notify the process of a serial line drop. SIGHUP is a symbolic constant defined in the head ...
to all jobs, and waits for the process groups to end before terminating itself.
The
disown
command can be used to remove jobs from the job table, so that when the session ends the child process groups are not sent SIGHUP, nor does the shell wait for them to terminate. They thus become
orphan process
An orphan process is a computer process whose parent process has finished or terminated, though it remains running itself.
Unix-like
In a Unix-like operating system any orphaned process will be immediately adopted by an implementation-defined s ...
es, and may be terminated by the operating system, though more often this is used so the processes are adopted by
init
In Unix-based computer operating systems, init (short for ''initialization'') is the first process started during booting of the computer system. Init is a daemon process that continues running until the system is shut down. It is the direct ...
(the kernel sets their parent process to init) and continue executing as
daemon
Daimon or Daemon (Ancient Greek: , "god", "godlike", "power", "fate") originally referred to a lesser deity or guiding spirit such as the daimons of ancient Greek religion and mythology and of later Hellenistic religion and philosophy.
The word ...
s. Alternatives to prevent jobs from being terminated include
nohup
nohup is a POSIX command which means "no hang up". Its purpose is to execute a command such that it ignores the HUP (hangup) signal and therefore does not stop when the user logs out.
Output that would normally go to the terminal goes to a file ...
and using a
terminal multiplexer
A terminal multiplexer is a software application that can be used to multiplex several separate pseudoterminal-based login sessions inside a single terminal display, terminal emulator window, PC/workstation system console, or remote login session, ...
.
A job running in the foreground can be stopped by typing the suspend character (
Ctrl-Z
In computer data, a substitute character (␚) is a control character that is used to pad transmitted data in order to send it in blocks of fixed size, or to stand in place of a character that is recognized to be invalid, erroneous or unreprese ...
). This sends the "terminal stop"
signal
In signal processing, a signal is a function that conveys information about a phenomenon. Any quantity that can vary over space or time can be used as a signal to share messages between observers. The ''IEEE Transactions on Signal Processing'' ...
(SIGTSTP) 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 "stop" signal (SIGSTOP), which cannot be caught or ignored.
A job running in the foreground can be interrupted by typing the interruption character (
Ctrl-C
Control+C is a common command (computing), computer command. It is generated by pressing the key while holding down the key on most computer keyboards.
In graphical user interface environments that use the control key to control the active pro ...
). This sends the "interrupt" signal (
SIGINT
Signals intelligence (SIGINT) is intelligence-gathering by interception of ''signals'', whether communications between people (communications intelligence—abbreviated to COMINT) or from electronic signals not directly used in communication ( ...
), which defaults to terminating the process, though it can be overridden.
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
Bash or BASH may refer to:
Arts and entertainment
* ''Bash!'' (Rockapella album), 1992
* ''Bash!'' (Dave Bailey album), 1961
* '' Bash: Latter-Day Plays'', a dramatic triptych
* ''BASH!'' (role-playing game), a 2005 superhero game
* "Bash" ('' ...
, a program can be started as a background job 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
The POSIX terminal interface is the generalized abstraction, comprising both an application programming interface for programs, and a set of behavioural expectations for users of a terminal (telecommunication), terminal, as defined by the POSIX sta ...
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 processes deliver their output to the controlling terminal by default.
In Bash-compatible shells, the
kill
Kill often refers to:
*Homicide, one human killing another
*cause death, to kill a living organism, to cause its death
Kill may also refer to:
Media
*'' Kill!'', a 1968 film directed by Kihachi Okamoto
* ''Kill'' (Cannibal Corpse album), 2006
* ...
builtin (not
/bin/kill
) can signal jobs by job ID as well as by process group ID – sending a signal to a job sends it to the whole process group, and 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
Signals are standardized messages sent to a running program to trigger specific behavior, such as quitting or error handling. They are a limited form of inter-process communication (IPC), typically used in Unix, Unix-like, and other POSIX-compli ...
and
SIGTERM
Signals are standardized messages sent to a running program to trigger specific behavior, such as quitting or error handling. They are a limited form of inter-process communication (IPC), typically used in Unix, Unix-like, and other POSIX-comp ...
(the default) are probably the most applicable.
See also
*
C shell
The C shell (csh or the improved version, tcsh) is a Unix shell created by Bill Joy while he was a graduate student at University of California, Berkeley in the late 1970s. It has been widely distributed, beginning with the 2BSD release of the ...
Notes
References
Further reading
* {{cite book,
url=http://www.informit.com/title/0201702452,
title=The Design and Implementation of the FreeBSD Operating System,
author=
Marshall Kirk McKusick
Marshall Kirk McKusick (born January 19, 1954) is a computer scientist, known for his extensive work on BSD UNIX, from the 1980s to FreeBSD in the present day. He was president of the USENIX Association from 1990 to 1992 and again from 2002 ...
and George V. Neville-Neil,
date=2004-08-02,
publisher=Addison Wesley,
chapter-url=http://www.informit.com/articles/article.aspx?p=366888&seqNum=8,
chapter=FreeBSD Process Management: Process Groups and Sessions,
isbn=0-201-70245-2
External links
* "Job Control"
''Bash Reference Manual''
Unix