Builtins builtin commands bg, fg, jobs, kill, disown, suspend, autoresume
Variables Variables Bash uses to customize job control.
Job control is the ability to suspend the execution of processes and resume execution at a later time.
When a pipeline started asynchronously ( using an &
at the end of the command line),
Bash associates a job with it
and displays the job number & process ID of the last process in the pipeline for example:
> ls -l |cut -c33- |sort -n |tail -n2 &
[1] 25647
All of the processes in a single pipeline are members of the same job.
A table of currently executing jobs, is listed using the jobs
command.
The operating system maintains a terminal process group , whose members
(processes whose process group ID is equal to the current terminal process group ID)
receive keyboard-generated signals such as sigINT
(by default ^C
) and are in the foreground.
Typing the susp
character (typically ^Z
, Control-Z)
causes the process to be suspended immediately,
pending output and typeahead to be discarded.
and control returns to Bash.
Typing the delayed suspend
character (typically ^Y
)
causes the process to be suspended when it attempts to read input from the terminal then control to be returned to Bash.
bg
to continue it in the background
fg
to continue it in the foreground
kill
sigTTIN
(sigTTOU
) signal, which suspends the process, (unless previoulsly trapped) ).
%
introduces a Job name or number n
may be referred to as %n
.
%%
and %+
refer to the current job, which is the last job stopped while it was in the foreground or started in the background.
%-
previous job jobs
flags the current job with a +
, and the previous job with a -
.
A job may also be referred to using a prefix of the name used to start it, or using a substring
that appears in its command line. For example, %ce
refers
to a stopped ce
job. Using %?ce
, on the
other hand, refers to any job containing the string ce
in
its command line. If the prefix or substring matches more than one job,
Bash reports an error.
Naming a job can be used to bring it into the foreground:
%1
is a synonym for fg %1
, bringing job 1 from the
background into the foreground. Similarly, %1 &
resumes
job 1 in the background, equivalent to bg %1
The shell is notified whenever a job changes state.
Normally, Bash waits until it is about to print a prompt
before reporting changes in a job's status so as to not interrupt any other output.
If the -b
option to the set
builtin is enabled,
Bash reports such changes immediately (see section Set Builtin).
Any trap on sigCHLD
is executed for each child process that exits.
Issuing an exit while the are stopped jobs causes Bash to display the warning:
There are stopped jobs.
and the exit is not performed.
jobs
displays their status.
A second exit causes the stopped jobs to be terminated, if there are no intervening commands.
> gzip 0908*
^Z
[1]+ Suspended gzip 0908*
> bg gzip
[1]+ gzip 0908* &
> jobs
[1]+ Running gzip 0908* &
> jobs
[1]+ Done gzip 0908*