Differences between revisions 1 and 2
Revision 1 as of 2008-06-24 20:51:55
Size: 3136
Editor: NeilMoore
Comment: New section on job control
Revision 2 as of 2008-06-24 20:52:16
Size: 3137
Editor: NeilMoore
Comment: fix up a heading
Deletions are marked like this. Additions are marked like this.
Line 20: Line 20:
=== Job Specifications == === Job Specifications ===

Anchor(JobControl)

Job Control

Though not typically used in scripts, job control is very important in interactive shells. Job control allows you to interact with background jobs, suspend foreground jobs, and so on.

On Posix systems, jobs are implemented as "process groups", with one process being the leader of the group. A job is suspended when its process group leader receives one of the signals SIGSTOP, SIGTSTP, SIGTTIN, or SIGTTOU. Pressing Ctrl-Z sends SIGTSTP to all the processes in the foreground job. SIGTTIN and SIGTTOU are automatically sent whenever a background job tries to read from or write to the terminal---this is why cat & is immediately suspended rather than running in the background.

TODO: Describe job control principles (process groups, sessions, foreground process group, tty signals, etc.) in more detail.

Job control can be turned on with set -m or set -o monitor. Job control enables the following commands:

  • fg [jobspec]: bring a suspended or background job to the foreground.

  • bg [jobspec ...]: run a suspended job in the background.

  • suspend: suspend the shell (mostly useful when the parent process is a shell with job control).

Other commands for interacting with jobs include:

  • jobs [options] [jobspec ...]: list suspended and background jobs. Options include -p (list process IDs only), -s (list only suspended jobs), and -r (list only running background jobs). If one or more jobspecs are specified, all other jobs are ignored.

  • kill can take a jobspec instead of a process ID.

  • disown tells bash to forget about an existing job. This keeps bash from automatically sending SIGHUP to the processes in that job, but also means it can no longer be referred to by jobspec.

Job Specifications

A job specification or "jobspec" is a way of referring to the processes that make up a job. A jobspec may be:

  • %n to refer to job number n.

  • %str to refer to a job which was started by a command beginning with str. It is an error if there is more than one such job.

  • %?str to refer to a job which was started by a command containing str. It is an error if there is more than one such job.

  • %% or %+ to refer to the current job: the one most recently started in the background, or suspended from the foreground. fg and bg will operate on this job if no jobspec is given.

  • %- for the previous job (the job that was %% before the current one).

It is possible to run an arbitrary command with a jobspec, using jobs -x ''cmd args...''. This replaces arguments that look like jobspecs with the PIDs of the corresponding process group leaders, then runs the command. For example, jobs -x strace -p %% will attach strace to the current job (most useful if it is running in the background rather than suspended).

Finally, a bare job spec may be used as a command: %1 is equivalent to fg %1, while %1 & is equivalent to bg %1.

BashGuide/JobControl (last edited 2019-09-30 10:00:56 by 151)