Difference between revisions of "Job control"

From Christoph's Personal Wiki
Jump to: navigation, search
 
(Process state codes)
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
When using [[Linux]] 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 lengthy 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 programs into the '''foreground''', and start and stop running programs. Programs under the influence of a job control facility are referred to as '''jobs'''.
+
When using [[Linux]] 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 lengthy 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 programs into the '''foreground''', and start and stop running programs. Programs under the influence of a job control facility are referred to as '''jobs'''.
  
 
== Implementation ==
 
== Implementation ==
* Typically, the shell keeps a list of jobs in a '''job table'''. A job consists of all the members of a [[pipeline (Unix)|pipeline]].
+
* Typically, the shell keeps a list of jobs in a '''job table'''. A job consists of all the members of a [[pipeline]].
* A program can be started as a background task by appending <code>&</code><ref>This section uses [[Bash]] syntax; other shells offer similar functionality under other names.</ref> 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 program can be started as a background task by appending <tt>&</tt><ref>This section uses [[Bash]] syntax; other shells offer similar functionality under other names.</ref> 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 task running in the foreground can be stopped by typing the suspend character (Ctrl+Z); this sends [[SIGTSTP]] to the process and returns control to the shell.  
 
* A task running in the foreground can be stopped by typing the suspend character (Ctrl+Z); this sends [[SIGTSTP]] to the process and returns control to the shell.  
* The process can be resumed as a background job with the <code>bg</code> [[shell builtin|builtin]] or as the foreground job with <code>fg</code>; in either case the shell redirects [[Input/output|I/O]] appropriately and sends [[SIGCONT]] to the process.  
+
* The process can be resumed as a background job with the <tt>bg</tt> [[Job control#Job Control Builtins|builtin]] or as the foreground job with <tt>fg</tt>; in either case the shell redirects I/O appropriately and sends [[SIGCONT]] to the process.  
* <code>jobs</code> will list the background jobs existing in the job table, along with their job number and job state (stopped or running).
+
* <tt>jobs</tt> will list the background jobs existing in the job table, along with their job number and job state (stopped or running).
* The <code>kill</code> builtin (not <code>/bin/kill</code>) can signal processes by job number as well as by process ID.
+
* The <tt>kill</tt> builtin (''not'' <code>/bin/kill</code>) can signal processes by job number as well as by process ID.
* <code>disown</code> can be used to remove jobs from the job table, converting them from jobs into [[daemon (computer software)|daemon]]s so that they continue executing when the user [[logout|logs out]].
+
* <tt>disown</tt> 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.
  
=== Job Control Basics ===
+
=== Job control basics ===
  
 
Job control refers to the ability to selectively stop (suspend) the execution of processes and continue (resume) their execution at a later point. A user typically employs this facility via an interactive interface supplied jointly by the system's terminal driver and Bash.
 
Job control refers to the ability to selectively stop (suspend) the execution of processes and continue (resume) their execution at a later point. A user typically employs this facility via an interactive interface supplied jointly by the system's terminal driver and Bash.
Line 18: Line 18:
 
  [1] 25647
 
  [1] 25647
  
indicating that this job is job number 1 and that the process ID of the last process in the pipeline associated with this job is 25647. All of the processes in a single pipeline are members of the same job. Bash uses the job abstraction as the basis for job control.
+
indicating that this job is job number 1 and that the process ID of the last process in the pipeline associated with this job is <code>25647</code>. All of the processes in a single pipeline are members of the same job. Bash uses the job abstraction as the basis for job control.
  
To facilitate the implementation of the user interface to job control, the operating system maintains the notion of a current terminal process group ID. Members of this process group (processes whose process group ID is equal to the current terminal process group ID) receive keyboard-generated signals such as SIGINT. These processes are said to be in the foreground. Background processes are those whose process group ID differs from the terminal's; such processes are immune to keyboard-generated signals. Only foreground processes are allowed to read from or write to the terminal. Background processes which attempt to read from (write to) the terminal are sent a SIGTTIN (SIGTTOU) signal by the terminal driver, which, unless caught, suspends the process.
+
To facilitate the implementation of the user interface to job control, the operating system maintains the notion of a current terminal process group ID. Members of this process group (processes whose process group ID is equal to the current terminal process group ID) receive keyboard-generated signals such as <code>SIGINT</code>. These processes are said to be in the foreground. Background processes are those whose process group ID differs from the terminal's; such processes are immune to keyboard-generated signals. Only foreground processes are allowed to read from or write to the terminal. Background processes which attempt to read from (write to) the terminal are sent a <code>SIGTTIN</code> (<code>SIGTTOU</code>) signal by the terminal driver, which, unless caught, suspends the process.
  
If the operating system on which Bash is running supports job control, Bash contains facilities to use it. Typing the suspend character (typically '^Z', Control-Z) while a process is running causes that process to be stopped and returns control to Bash. Typing the delayed suspend character (typically '^Y', Control-Y) causes the process to be stopped when it attempts to read input from the terminal, and control to be returned to Bash. The user then manipulates the state of this job, using the bg command to continue it in the background, the fg command to continue it in the foreground, or the kill command to kill it. A '^Z' takes effect immediately, and has the additional side effect of causing pending output and typeahead to be discarded.
+
If the operating system on which Bash is running supports job control, Bash contains facilities to use it. Typing the suspend character (typically '<tt>^Z</tt>', Control-Z) while a process is running causes that process to be stopped and returns control to Bash. Typing the delayed suspend character (typically '<tt>^Y</tt>', Control-Y) causes the process to be stopped when it attempts to read input from the terminal, and control to be returned to Bash. The user then manipulates the state of this job, using the <tt>bg</tt> command to continue it in the background, the <tt>fg</tt> command to continue it in the foreground, or the kill command to kill it. A '<tt>^Z</tt>' takes effect immediately, and has the additional side effect of causing pending output and typeahead to be discarded.
  
There are a number of ways to refer to a job in the shell. The character `%' introduces a job name.
+
There are a number of ways to refer to a job in the shell. The character '<code>%</code>' introduces a job name.
  
Job number n may be referred to as '%n'. The symbols '%%' and '%+' refer to the shell's notion of the current job, which is the last job stopped while it was in the foreground or started in the background. The previous job may be referenced using '%-'. In output pertaining to jobs (e.g., the output of the jobs command), the current job is always flagged with a '+', and the previous job with a '-'.
+
Job number n may be referred to as '<code>%n</code>'. The symbols '<code>%%</code>' and '<code>%+</code>' refer to the shell's notion of the current job, which is the last job stopped while it was in the foreground or started in the background. The previous job may be referenced using '<code>%-</code>'. In output pertaining to jobs (eg, the output of the jobs command), the current job is always flagged with a '<code>+</code>', and the previous job with a '<code>-</code>'.
  
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.
+
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, '<code>%ce</code>' refers to a stopped ce job. Using '<code>%?ce</code>', on the other hand, refers to any job containing the string '<code>ce</code>' in its command line. If the prefix or substring matches more than one job, Bash reports an error.
  
Simply 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'
+
Simply naming a job can be used to bring it into the foreground: '<code>%1</code>' is a synonym for '<code>fg %1</code>', bringing job 1 from the background into the foreground. Similarly, '<code>%1 &</code>' resumes job 1 in the background, equivalent to '<code>bg %1</code>'.
  
The shell learns immediately 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 4.3 The Set Builtin). Any trap on SIGCHLD is executed for each child process that exits.
+
The shell learns immediately 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 '<code>-b</code>' option to the set builtin is enabled, Bash reports such changes immediately (see: "The Set Builtin"). Any trap on <code>SIGCHLD</code> is executed for each child process that exits.
  
 
If an attempt to exit Bash is while jobs are stopped, the shell prints a message warning that there are stopped jobs. The jobs command may then be used to inspect their status. If a second attempt to exit is made without an intervening command, Bash does not print another warning, and the stopped jobs are terminated.  
 
If an attempt to exit Bash is while jobs are stopped, the shell prints a message warning that there are stopped jobs. The jobs command may then be used to inspect their status. If a second attempt to exit is made without an intervening command, Bash does not print another warning, and the stopped jobs are terminated.  
  
=== Job Control Builtins ===
+
=== Job control builtins ===
  
 
* '''bg'''
 
* '''bg'''
 
:: <tt>bg [jobspec]</tt>
 
:: <tt>bg [jobspec]</tt>
:: Resume the suspended job jobspec in the background, as if it had been started with `&'. If jobspec is not supplied, the current job is used. The return status is zero unless it is run when job control is not enabled, or, when run with job control enabled, if jobspec was not found or jobspec specifies a job that was started without job control.
+
:: Resume the suspended job <code>jobspec</code> in the background, as if it had been started with '<code>&</code>'. If <code>jobspec</code> is not supplied, the current job is used. The return status is zero unless it is run when job control is not enabled, or, when run with job control enabled, if <code>jobspec</code> was not found or <code>jobspec</code> specifies a job that was started without job control.
  
 
* '''fg'''
 
* '''fg'''
 
:: <tt>fg [jobspec]</tt>
 
:: <tt>fg [jobspec]</tt>
:: Resume the job jobspec in the foreground and make it the current job. If jobspec is not supplied, the current job is used. The return status is that of the command placed into the foreground, or non-zero if run when job control is disabled or, when run with job control enabled, jobspec does not specify a valid job or jobspec specifies a job that was started without job control.
+
:: Resume the job <code>jobspec</code> in the foreground and make it the current job. If jobspec is not supplied, the current job is used. The return status is that of the command placed into the foreground, or non-zero if run when job control is disabled or, when run with job control enabled, <code>jobspec</code> does not specify a valid job or <code>jobspec</code> specifies a job that was started without job control.
  
 
* '''jobs'''
 
* '''jobs'''
Line 60: Line 60:
 
::: <tt>-s</tt>
 
::: <tt>-s</tt>
 
:::: Restrict output to stopped jobs.  
 
:::: Restrict output to stopped jobs.  
:: If jobspec is given, output is restricted to information about that job. If jobspec is not supplied, the status of all jobs is listed.
+
:: If <code>jobspec</code> is given, output is restricted to information about that job. If <code>jobspec</code> is not supplied, the status of all jobs is listed.
:: If the `-x' option is supplied, jobs replaces any jobspec found in command or arguments with the corresponding process group ID, and executes command, passing it arguments, returning its exit status.
+
:: If the '<code>-x</code>' option is supplied, jobs replaces any <code>jobspec</code> found in command or arguments with the corresponding process group ID, and executes command, passing it arguments, returning its exit status.
  
 
* '''kill'''
 
* '''kill'''
 
:: <tt>kill [-s sigspec] [-n signum] [-sigspec] jobspec or pid</tt>
 
:: <tt>kill [-s sigspec] [-n signum] [-sigspec] jobspec or pid</tt>
 
:: <tt>kill -l [exit_status]</tt>
 
:: <tt>kill -l [exit_status]</tt>
:: Send a signal specified by sigspec or signum to the process named by job specification jobspec or process ID pid. sigspec is either a signal name such as SIGINT (with or without the SIG prefix) or a signal number; signum is a signal number. If sigspec and signum are not present, SIGTERM is used. The `-l' option lists the signal names. If any arguments are supplied when `-l' is given, the names of the signals corresponding to the arguments are listed, and the return status is zero. exit_status is a number specifying a signal number or the exit status of a process terminated by a signal. The return status is zero if at least one signal was successfully sent, or non-zero if an error occurs or an invalid option is encountered.
+
:: Send a signal specified by sigspec or signum to the process named by job specification <code>jobspec</code> or process ID <code>pid</code>. <code>sigspec</code> is either a signal name such as <code>SIGINT</code> (with or without the <code>SIG</code> prefix) or a signal number; <code>signum</code> is a signal number. If <code>sigspec</code> and <code>signum</code> are not present, <code>SIGTERM</code> is used. The '<code>-l</code>' option lists the signal names. If any arguments are supplied when '<code>-l</code>' is given, the names of the signals corresponding to the arguments are listed, and the return status is zero. <code>exit_status</code> is a number specifying a signal number or the exit status of a process terminated by a signal. The return status is zero if at least one signal was successfully sent, or non-zero if an error occurs or an invalid option is encountered.
  
 
* '''wait'''
 
* '''wait'''
 
:: <tt>wait [jobspec or pid]</tt>
 
:: <tt>wait [jobspec or pid]</tt>
:: Wait until the child process specified by process ID pid or job specification jobspec exits and return the exit status of the last command waited for. If a job spec is given, all processes in the job are waited for. If no arguments are given, all currently active child processes are waited for, and the return status is zero. If neither jobspec nor pid specifies an active child process of the shell, the return status is 127.
+
:: Wait until the child process specified by process ID <code>pid</code> or job specification <code>jobspec</code> exits and return the exit status of the last command waited for. If a <code>jobspec</code> is given, all processes in the job are waited for. If no arguments are given, all currently active child processes are waited for, and the return status is zero. If neither <code>jobspec</code> nor <code>pid</code> specifies an active child process of the shell, the return status is <code>127</code>.
  
 
* '''disown'''
 
* '''disown'''
 
:: <tt>disown [-ar] [-h] [jobspec ...]</tt>
 
:: <tt>disown [-ar] [-h] [jobspec ...]</tt>
:: Without options, each jobspec is removed from the table of active jobs. If the `-h' option is given, the job is not removed from the table, but is marked so that SIGHUP is not sent to the job if the shell receives a SIGHUP. If jobspec is not present, and neither the `-a' nor `-r' option is supplied, the current job is used. If no jobspec is supplied, the `-a' option means to remove or mark all jobs; the `-r' option without a jobspec argument restricts operation to running jobs.
+
:: Without options, each jobspec is removed from the table of active jobs. If the '<code>-h</code>' option is given, the job is not removed from the table, but is marked so that <code>SIGHUP</code> is not sent to the job if the shell receives a <code>SIGHUP</code>. If <code>jobspec</code> is not present, and neither the '<code>-a</code>' nor '<code>-r</code>' option is supplied, the current job is used. If no <code>jobspec</code> is supplied, the '<code>-a</code>' option means to remove or mark all jobs; the '<code>-r</code>' option without a <code>jobspec</code> argument restricts operation to running jobs.
  
 
* '''suspend'''
 
* '''suspend'''
 
:: <tt>suspend [-f]</tt>
 
:: <tt>suspend [-f]</tt>
:: Suspend the execution of this shell until it receives a SIGCONT signal. The `-f' option means to suspend even if the shell is a login shell.
+
:: Suspend the execution of this shell until it receives a <code>SIGCONT</code> signal. The '<code>-f</code>' option means to suspend even if the shell is a login shell.
  
When job control is not active, the kill and wait builtins do not accept jobspec arguments. They must be supplied process IDs.
+
* '''set'''
 +
:: (see: [http://cnswww.cns.cwru.edu/~chet/bash/bashref.html#SEC59 Bash Reference Manual])
  
=== Job Control Variables ===
+
When job control is not active, the <tt>kill</tt> and <tt>wait</tt> builtins do not accept <code>jobspec</code> arguments. They must be supplied process IDs.
  
; <tt>auto_resume</tt> : This variable controls how the shell interacts with the user and job control. If this variable exists then single word simple commands without redirections are treated as candidates for resumption of an existing job. There is no ambiguity allowed; if there is more than one job beginning with the string typed, then the most recently accessed job will be selected. The name of a stopped job, in this context, is the command line used to start it. If this variable is set to the value `exact', the string supplied must match the name of a stopped job exactly; if set to `substring', the string supplied needs to match a substring of the name of a stopped job. The `substring' value provides functionality analogous to the `%?' job ID (see section 7.1 Job Control Basics). If set to any other value, the supplied string must be a prefix of a stopped job's name; this provides functionality analogous to the `%' job ID.
+
=== Job control variables ===
 +
 
 +
; <tt>auto_resume</tt> : This variable controls how the shell interacts with the user and job control. If this variable exists then single word simple commands without redirections are treated as candidates for resumption of an existing job. There is no ambiguity allowed; if there is more than one job beginning with the string typed, then the most recently accessed job will be selected. The name of a stopped job, in this context, is the command line used to start it. If this variable is set to the value '<code>exact</code>', the string supplied must match the name of a stopped job exactly; if set to '<code>substring</code>', the string supplied needs to match a substring of the name of a stopped job. The '<code>substring</code>' value provides functionality analogous to the '<code>%?</code>' job ID (see [[Job control#Job Control Basics|Job Control Basics]]). If set to any other value, the supplied string must be a prefix of a stopped job's name; this provides functionality analogous to the '<code>%</code>' job ID.
 +
 
 +
==Process state codes==
 +
''Note: See <code>man ps</code> for complete list; under "PROCESS STATE CODES" section.''
 +
;R : runnable
 +
;S : stopped (usually because it is waiting on input or doing something else that does not require processing time from the CPU)
 +
;D : uninterruptible IO wait (usually requires a reboot to clear)
 +
;T : process is stopped (send a SIGCONT to start the process again)
 +
;Z : process is defunct (either kill the parent or reboot)
 +
 
 +
==Signals==
 +
''Note: Look in <code>/usr/include/asm/signal.h</code> for a complete list of signals, or execute <code>kill -l</code>.''
 +
*<code> 2 SIGBUS</code> &mdash; Bus Error
 +
*<code> 9 SIGKILL</code> &mdash; Terminate a process. Noninterruptible.
 +
*<code>11 SIGSEGV</code> &mdash; Segmentation Fault.
 +
*<code>15 SIGTERM</code> &mdash; Terminate a process in an orderly fashion.
 +
*<code>17 SIGCHLD</code> &mdash; Child process terminated
 +
*<code>18 SIGCONT</code> &mdash; Continue executing after a stop
 +
*<code>19 SIGSTOP</code> &mdash; Stop executing
  
 
== See also ==
 
== See also ==
 +
*[[nohup]]
 +
*[[ps]]
 +
 +
== References and notes ==
 +
<references/>
 +
 +
== External links ==
 
* [http://cnswww.cns.cwru.edu/~chet/bash/bashref.html#SEC87 Job control in Bash]
 
* [http://cnswww.cns.cwru.edu/~chet/bash/bashref.html#SEC87 Job control in Bash]
  
 
[[Category:Linux Command Line Tools]]
 
[[Category:Linux Command Line Tools]]

Latest revision as of 04:42, 21 April 2023

When using Linux 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 lengthy 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 programs into the foreground, and start and stop running programs. Programs under the influence of a job control facility are referred to as jobs.

Implementation

  • Typically, the shell keeps a list of jobs in a job table. A job consists of all the members of a pipeline.
  • A program can be started as a background task by appending &[1] 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 task running in the foreground can be stopped by typing the suspend character (Ctrl+Z); this sends SIGTSTP to the process and returns control to the shell.
  • The process 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 SIGCONT to the process.
  • jobs will list the background jobs existing in the job table, along with their job number and job state (stopped or running).
  • The kill builtin (not /bin/kill) can signal processes by job number as well as by process ID.
  • disown 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.

Job control basics

Job control refers to the ability to selectively stop (suspend) the execution of processes and continue (resume) their execution at a later point. A user typically employs this facility via an interactive interface supplied jointly by the system's terminal driver and Bash.

The shell associates a job with each pipeline. It keeps a table of currently executing jobs, which may be listed with the jobs command. When Bash starts a job asynchronously, it prints a line that looks like:

[1] 25647

indicating that this job is job number 1 and that the process ID of the last process in the pipeline associated with this job is 25647. All of the processes in a single pipeline are members of the same job. Bash uses the job abstraction as the basis for job control.

To facilitate the implementation of the user interface to job control, the operating system maintains the notion of a current terminal process group ID. Members of this process group (processes whose process group ID is equal to the current terminal process group ID) receive keyboard-generated signals such as SIGINT. These processes are said to be in the foreground. Background processes are those whose process group ID differs from the terminal's; such processes are immune to keyboard-generated signals. Only foreground processes are allowed to read from or write to the terminal. Background processes which attempt to read from (write to) the terminal are sent a SIGTTIN (SIGTTOU) signal by the terminal driver, which, unless caught, suspends the process.

If the operating system on which Bash is running supports job control, Bash contains facilities to use it. Typing the suspend character (typically '^Z', Control-Z) while a process is running causes that process to be stopped and returns control to Bash. Typing the delayed suspend character (typically '^Y', Control-Y) causes the process to be stopped when it attempts to read input from the terminal, and control to be returned to Bash. The user then manipulates the state of this job, using the bg command to continue it in the background, the fg command to continue it in the foreground, or the kill command to kill it. A '^Z' takes effect immediately, and has the additional side effect of causing pending output and typeahead to be discarded.

There are a number of ways to refer to a job in the shell. The character '%' introduces a job name.

Job number n may be referred to as '%n'. The symbols '%%' and '%+' refer to the shell's notion of the current job, which is the last job stopped while it was in the foreground or started in the background. The previous job may be referenced using '%-'. In output pertaining to jobs (eg, the output of the jobs command), the current job is always flagged 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.

Simply 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 learns immediately 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: "The Set Builtin"). Any trap on SIGCHLD is executed for each child process that exits.

If an attempt to exit Bash is while jobs are stopped, the shell prints a message warning that there are stopped jobs. The jobs command may then be used to inspect their status. If a second attempt to exit is made without an intervening command, Bash does not print another warning, and the stopped jobs are terminated.

Job control builtins

  • bg
bg [jobspec]
Resume the suspended job jobspec in the background, as if it had been started with '&'. If jobspec is not supplied, the current job is used. The return status is zero unless it is run when job control is not enabled, or, when run with job control enabled, if jobspec was not found or jobspec specifies a job that was started without job control.
  • fg
fg [jobspec]
Resume the job jobspec in the foreground and make it the current job. If jobspec is not supplied, the current job is used. The return status is that of the command placed into the foreground, or non-zero if run when job control is disabled or, when run with job control enabled, jobspec does not specify a valid job or jobspec specifies a job that was started without job control.
  • jobs
jobs [-lnprs] [jobspec]
jobs -x command [arguments]
The first form lists the active jobs. The options have the following meanings:
-l
List process IDs in addition to the normal information.
-n
Display information only about jobs that have changed status since the user was last notified of their status.
-p
List only the process ID of the job's process group leader.
-r
Restrict output to running jobs.
-s
Restrict output to stopped jobs.
If jobspec is given, output is restricted to information about that job. If jobspec is not supplied, the status of all jobs is listed.
If the '-x' option is supplied, jobs replaces any jobspec found in command or arguments with the corresponding process group ID, and executes command, passing it arguments, returning its exit status.
  • kill
kill [-s sigspec] [-n signum] [-sigspec] jobspec or pid
kill -l [exit_status]
Send a signal specified by sigspec or signum to the process named by job specification jobspec or process ID pid. sigspec is either a signal name such as SIGINT (with or without the SIG prefix) or a signal number; signum is a signal number. If sigspec and signum are not present, SIGTERM is used. The '-l' option lists the signal names. If any arguments are supplied when '-l' is given, the names of the signals corresponding to the arguments are listed, and the return status is zero. exit_status is a number specifying a signal number or the exit status of a process terminated by a signal. The return status is zero if at least one signal was successfully sent, or non-zero if an error occurs or an invalid option is encountered.
  • wait
wait [jobspec or pid]
Wait until the child process specified by process ID pid or job specification jobspec exits and return the exit status of the last command waited for. If a jobspec is given, all processes in the job are waited for. If no arguments are given, all currently active child processes are waited for, and the return status is zero. If neither jobspec nor pid specifies an active child process of the shell, the return status is 127.
  • disown
disown [-ar] [-h] [jobspec ...]
Without options, each jobspec is removed from the table of active jobs. If the '-h' option is given, the job is not removed from the table, but is marked so that SIGHUP is not sent to the job if the shell receives a SIGHUP. If jobspec is not present, and neither the '-a' nor '-r' option is supplied, the current job is used. If no jobspec is supplied, the '-a' option means to remove or mark all jobs; the '-r' option without a jobspec argument restricts operation to running jobs.
  • suspend
suspend [-f]
Suspend the execution of this shell until it receives a SIGCONT signal. The '-f' option means to suspend even if the shell is a login shell.
  • set
(see: Bash Reference Manual)

When job control is not active, the kill and wait builtins do not accept jobspec arguments. They must be supplied process IDs.

Job control variables

auto_resume 
This variable controls how the shell interacts with the user and job control. If this variable exists then single word simple commands without redirections are treated as candidates for resumption of an existing job. There is no ambiguity allowed; if there is more than one job beginning with the string typed, then the most recently accessed job will be selected. The name of a stopped job, in this context, is the command line used to start it. If this variable is set to the value 'exact', the string supplied must match the name of a stopped job exactly; if set to 'substring', the string supplied needs to match a substring of the name of a stopped job. The 'substring' value provides functionality analogous to the '%?' job ID (see Job Control Basics). If set to any other value, the supplied string must be a prefix of a stopped job's name; this provides functionality analogous to the '%' job ID.

Process state codes

Note: See man ps for complete list; under "PROCESS STATE CODES" section.

runnable
stopped (usually because it is waiting on input or doing something else that does not require processing time from the CPU)
uninterruptible IO wait (usually requires a reboot to clear)
process is stopped (send a SIGCONT to start the process again)
process is defunct (either kill the parent or reboot)

Signals

Note: Look in /usr/include/asm/signal.h for a complete list of signals, or execute kill -l.

  • 2 SIGBUS — Bus Error
  • 9 SIGKILL — Terminate a process. Noninterruptible.
  • 11 SIGSEGV — Segmentation Fault.
  • 15 SIGTERM — Terminate a process in an orderly fashion.
  • 17 SIGCHLD — Child process terminated
  • 18 SIGCONT — Continue executing after a stop
  • 19 SIGSTOP — Stop executing

See also

References and notes

  1. This section uses Bash syntax; other shells offer similar functionality under other names.

External links