Job Control

Builtinsbg, fg, jobs, kill, disown, suspend, $AUTORESUME
Variablesto customize job control.

When a command is started a terminal process group is established in the foreground.
Keystrokes entered are buffered as type-a-head except for keyboard-generated signals including:

A command ending with & runs asynchronously, bash associates a job with it and displays the job number within [ ] and process ID of the last process in the pipeline .

For example:

% (wc -l /var/log/system.log; head -1  /var/log/system.log; echo +++; tail -f /var/log/system.log )& 
[2] 15448
     228 /var/log/system.log
Jul 17 03:10:04 smacpro syslogd[173]: ASL Sender Statistics 
+++
    ASL Module "com.apple.iokit.power" claims selected messages.
    Those messages may not appear in standard system log files or in the ASL database.
Jul 17 16:30:00 smacpro syslogd[173]: Configuration Notice:
    ASL Module "com.apple.mkb" sharing output destination "/private/var/log/keybagd.log" with ASL Module "com.apple.mkb.internal".
    Output parameters from ASL Module "com.apple.mkb.internal" override any specified in ASL Module "com.apple.mkb".
Background processes attempting to read from (write to) the terminal are sent sigTTIN (sigTTOU) which suspends the process, (unless trapped) ).

Currently executing jobs are listed using jobs.

Jobspec

A job may be referred to using a % and a prefix of the name used to start it or a substring in its command line. For example, %monthly refers to a stopped monthlyProductionSummary job.
Using %?Sum, refers to a job containing the string Sum in its command line.
Matching more than one job is an error.

A job may be referred to using %n.

Naming a job can be used to bring it into the foreground.
%n is a synonym for fg %n, bringing job n to the foreground.
%1 & resumes job 1 in the background, equivalent to bg %1

The shell is notified whenever a job changes state.
Bash (but not zsh) waits until it is about to output a prompt before reporting changes in a job's status so as to not interfer with other output, unless set -b is enabled

trap ' echo "child exited"' CHLD is executed for each child process that exits.

trap ' echo "child exited.\n\n"' CHLD; sleep 3 & 
ls -lR
…
[1]  + done       sleep 3
dgerman@smac14 dgerman % child exited.

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.

The current job is suffixed with a +, and the previous job with a -. A second exit causes the stopped jobs to be terminated, if there are no intervening commands.

 > find . -name 0908
^Z                   user  enters ^z
[1]+  Suspended               find . -name 0908 0908*
 > bg find . -name 0908
[1]+ find . -name 0908 0908* &
 > jobs
[1]+  Running                 find . -name 0908 0908* &
[1]+  Done                    find . -name 0908 0908*

Job Control Builtins

bg [jobspec] Resume suspended jobspec in the background, as if it had been started with &. Default the current job.
The return status is 0 unless
job control is not enabled, or
if jobspec was not found or specifies a job that was started without job control.

fg [jobspec] Resume jobspec in the foreground and make it the current job.
Default: current job.
The return status is that of the command placed into the foreground,
non-zero if run when job control is disabled or      job control is not enabled
jobspec does not specify a valid job or
a job that was started without job control.

jobs [-lnprs] [jobspec]
jobs -x command [args]
Without options, lists the active jobs.
> jobs
[1]+  Running   tail -f /var/log/system.log &  (wd: ~) workingDirectory of job
-l include process IDs.
> jobs -l
[1]+ 11612 Running                 tail -f /var/log/system.log & 
-n only jobs that have changed status
-p only the process ID of the job's process group leader.
> jobs -p
11612
-r running jobs.
-s [jobspec]status of stopped jobs.
With jobspec output is only for that job.
-x jobs replaces any jobspec found in command or args with the corresponding process group ID, and executes command, passing it arguments, returning its exit status.

kill [-s signame] jobspec
     [-signame]   
or
     [-n signum]   pid
Report signal signame or signum to the process named by jobspec or process ID pid.
Default signame: TERM.
shutdown reports quit then term.
When job control is not active, specify process IDs.
kill -l [exit_status] list signals corresponding to the arguments.
exit_status is a number specifying a signal number or the exit status of a process terminated by a signal.

 1 SIGHUP      2 SIGINT       3 SIGQUIT     4 SIGILLegal
 5 SIGTRAP      6 SIGABRT      7 SIGEMT       8 SIGFPE
 9 SIGKILL     10 SIGBUS      11 SIGSEGV     12 SIGSYS
13 SIGPIPE     14 SIGALRM     15 SIGTERM     16 SIGURG
17 SIGSTOP     18 SIGTSTP     19 SIGCONT     20 SIGCHLD
21 SIGTTIN     22 SIGTTOU     23 SIGIO       24 SIGXCPU
25 SIGXFSZ     26 SIGVTALRM   27 SIGPROF     28 SIGWINCH
29 SIGINFO    30 SIGUSR1     31 SIGUSR2 

C lib signal information
trap

wait [jobspec |
        pid] | -n With jobspec or pid
wait for it to exit. -n waits for any child process
Return status is that of last command waited.
If argument does not specify a child process, return status is 127.
When job control is not active, supply process IDs
disown [-ar]
        [
-h][jobspec]
Each jobspec is removed from the table of active jobs.
-h sigHUP is not sent to the job if the shell receives a sigHUP. current job .
-a all
-r running jobs.
% ping-with-time dapip
…
host:dapi2
18:17:15 18:17:23 18:17:31 18:17:39 18:17:472
^z
zsh: suspended  ping-with-time dapi2
jobs
[1]  + suspended  ping-with-time dapi2
% disown  ping-with-time dapi2
disown: warning: job is suspended, use `kill -CONT -18651' to resume 
suspend [-f] Suspend execution of this shell until it receives a sigCONT signal.
-f forces suspend even if a login shell.

Job Control Variables

$AUTO_RESUME
  exact |substring |prefix
If $AUTO_RESUME exists then single_word_simple commands without redirections are treated as candidates for resumption of an existing job.
No ambiguity is allowed, if there is more than one job beginning with the string entered, 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 set to 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 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.
top

bash table of contents