Differences between revisions 7 and 8
Revision 7 as of 2008-11-22 14:08:53
Size: 4025
Editor: localhost
Comment: converted to 1.6 markup
Revision 8 as of 2009-12-28 17:01:33
Size: 4082
Editor: GreyCat
Comment: restructuring the guide
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
## page was renamed from BashGuide/TheBasics/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.

Theory

On Posix systems, jobs are implemented as "process groups", with one process being the leader of the group. Each tty (terminal) has a single "foreground process group" that is allowed to interact with the terminal. All other process groups with the same controlling tty are considered background job, and can be either running or suspended.

A job is suspended when its process group leader receives one of the signals SIGSTOP, SIGTSTP, SIGTTIN, or SIGTTOU. 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.

Certain keypresses at the terminal cause signals to be sent to all processes in the foreground process group. These can be configured with stty, but are usually set to the defaults:

  • Ctrl-Z sends SIGTSTP to the foreground job (usually suspending it)

  • Ctrl-C sends SIGINT to the foreground job (usually terminating it)

  • Ctrl-\ sends SIGQUIT to the foreground job (usually causing it to dump core and abort)

Practice

Job control can be turned on with set -m or set -o monitor.

A foreground job can be suspended by pressing Ctrl-Z. There is no way to refer to the foreground job in bash: if there is a foreground job other than bash, then bash is waiting for that job to terminate, and hence cannot execute any code (even traps are delayed until the foreground job terminates). The following commands, therefore, work on background (and suspended) jobs only.

Job control enables the following commands:

  • fg [jobspec]: bring a 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 jobspec 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)