bg, fg, jobs, kill, disown, suspend, autoresume
|Variables||to customize job control.
When a command† is started
a terminal process group is
in the foreground and it receives keyboard-generated signals including:
the process to be suspended immediately.
Pending output and typeahead to be discarded and control returns to Bash.
^Y: delayed suspend:
the process to be suspended when it attempts to read from the terminal then control to be returned to Bash.
A command with an
& at the end of the line runs
Bash associates a job with it,
displays the job number within
[ ] and process ID of the last process in the pipeline
and continues executing the job in the background.
> (wc -l /var/log/system.log; head -1 /var/log/system.log; echo ...; tail -f /var/log/system.log )&
Jul 17 03:10:04 smacpro syslogd: 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: 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
sigTTOU) which suspends the process, (unless trapped) ).
A suspended job will be continued via:
bg in background
fg in foreground
Currently executing jobs are listed using
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,
to a stopped
%?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
%+ refer to the current job, which is the last job stopped while it was in the foreground or started in the background.
%- previous job
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
The shell is notified whenever a job changes state.
Bash waits until it is about to output a prompt
before reporting changes in a job's status so as to not interrupt any other output.
set -b is enabled, Bash reports such changes immediately.
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.
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
+ Suspended find . -name 0908 0908*
> bg find . -name 0908
+ find . -name 0908 0908* &
+ Running find . -name 0908 0908* &
+ Done find . -name 0908 0908*
Job Control Builtins
|Resume suspended |
jobspec in the background, as if it had been started with
&. Default the current job.
The return status is zero unless it is run when job control is not enabled, if
not found or specifies a job that was started without job control.
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 when run with
job control 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.
+ Running tail -f /var/log/system.log & (wd: ~) workingDirectory of job
|include process IDs.
> jobs -l
+ 11612 Running tail -f /var/log/system.log &
|only jobs that have changed status
|only the process ID of the job's process group leader.|
> jobs -p
|status of stopped jobs. |
jobspec output is only for that job.
jobs replaces any
jobspec found in
args with the
corresponding process group ID, and executes
arguments, returning its exit status.
kill [-s signame] jobspec or
[-n signum] pid
|Report signal |
signum to the process
jobspec or process ID
When job control is not active,
kill must be supplied process IDs not
kill -l [exit_status]
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.
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
wait [jobspec |
| Return status is zero.|
wait for it to exit. Return status is that of last command waited for.
If argument does not specify a child process, return status is 127.
When job control is not active,
wait must be supplied process IDs not jobspec.
[-h][jobspec … ]
jobspec is removed from the table of active jobs.
sigHUP is not sent to the job if the shell receives a
current job .
-r running jobs.
|Suspend the execution of this shell until it receives a
-f forces suspend even if the shell is a login shell.
Job Control Variables
auto_resume 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
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.
bash table of contents