Job control (Unix): Difference between revisions

Content deleted Content added
This section is about implementation, but not in general; other sections are also about implementation; this section is about signals; low level implementation
 
(5 intermediate revisions by one other user not shown)
Line 31:
 
==Control==
[[POSIX]] specifies the [[user interface]] to job control {{endash}} modeled on the Korn shell.<ref>{{man|cu|bg|SUS}}; {{man|cu|fg|SUS}}.</ref>. The commands are typically implemented as [[shell builtin]]s; not separate [[computer program |programs]].
 
; Start in background
:If a command line ends with {{code|&}}, then the job starts in the background.
 
; Pause foreground job
:The foreground job can be paused by pressing {{keypress |ControlCtrl| Z}}. In this state, a job can be resumed in the background via {{code |bg}} or resumed in the foreground via {{code |fg}}.
; Command {{code |fg}} {{anchor| fg}}
 
:Command {{code |fg}} (short for foreground) causes amoves background job to move to the foreground; either the job specified or the one most recently added to the background if none specified. When the foreground job is paused (via {{keypress |Ctrl| Z}}), then this command resumes that job.
; Command {{code|fg}}
; Command {{code |bgwait}}
Command {{code|fg}} (short for foreground) causes a background job to move to the foreground; either the job specified or the one most recently added to the background if none specified.
:Command [[wait (command)|<code>wait</code>]] pauses the interactive session until the specified background jobs complete or for all background jobs of the active shell if none specified.<ref>{{cite web | last=Kerrisk| first=Michael |date=Feb 2, 2025 |title=wait(1p) — Linux manual page |website=man7.org |url=https://www.man7.org/linux/man-pages/man1/wait.1p.html |publisher= |access-date=May 13, 2025}}</ref>
 
; Command {{code |waitbg}} {{anchor| bg}}
:Command {{code |bg}} (short for background) moves the paused foreground job to the background and resumes it.
Command [[wait (command)|<code>wait</code>]] pauses the interactive session until the specified background jobs complete or for all background jobs of the active shell if none specified.<ref>{{cite web | last=Kerrisk| first=Michael |date=Feb 2, 2025 |title=wait(1p) — Linux manual page |website=man7.org |url=https://www.man7.org/linux/man-pages/man1/wait.1p.html |publisher= |access-date=May 13, 2025}}</ref>
; Command {{code |jobs}}
 
:Command {{code |jobs}} reports information about each background job including ID, command line and running status (stopped or running).
; Command {{code|bg}}
Command {{code|bg}} (short for background) moves the paused foreground job to the background and resumes it.
 
; Command {{code|jobs}}
Command {{code|jobs}} reports information about each background job including ID, command line and running status (stopped or running).
 
==Signals==
{{Unreferenced section|date=February 2020}}
The [[interprocess communication]] of job control is implemented via [[Unix signal |signal]]s.
Typically, a shell maintains information about background jobs in a job table. When an [[login session |interactive session]] ends (i.e. user [[logout |logs out]]), the shell sends [[signal (computing)|signal]] [[SIGHUP]] to all jobs, and waits for the process groups to exit before terminating itself. Some shells provide a non-POSIX command [[Disown (Unix)|<code>disown</code>]] that removes a job from the job table. The process group becomes an [[orphan process |orphan]]. The shell will not send it SIGHUP, nor wait for it to terminate. This is one technique for enabling a process as a [[daemon (computer software)|daemon]]; owned direclty by the root process [[init]]. The POSIX command [[nohup |{{code|nohup}}]] provides an alternate way to prevent a job from being terminated by the shell.
 
Typically, a shell maintains information about background jobs in a job table. When an [[login session |interactive session]] ends (i.e. user [[logout |logs out]]), the shell sends [[signal (computing)|signal]] [[SIGHUP]] to all jobs, and waits for the process groups to exit before terminating itself. Some shells provide a non-POSIX command [[Disown (Unix)|<code>disown</code>]] that removes a job from the job table. The process group becomes an [[orphan process |orphan]]. The shell will not send it SIGHUP, nor wait for it to terminate. This is one technique for enabling a process as a [[daemon (computer software)|daemon]]; owned direclty by the root process [[init]]. The POSIX command [[nohup |{{code |nohup}}]] provides an alternate way to prevent a job from being terminated by the shell.
 
Suspending the foreground job (via {{keypress |Ctrl |Z}}) sends signal SIGTSTP (terminal stop) to the processes of the group. By default, this signal causes a process to pause so that the shell can resume. However, a process can ignore the signal. A process can also be paused via signal SIGSTOP (stop), which cannot be ignored.
Line 59 ⟶ 56:
When the user presses {{keypress |Ctrl |C}}, the shell sends signal [[SIGINT (POSIX)|SIGINT]] (interrupt) to each foreground job process, which defaults to terminating it, though a process can ignore the signal.
 
When a stopped job is resumed (via {{code| |bg}} or {{code| |fg}}), the shell redirects [[Input/output]] and resumes it by sending signal SIGCONT to it.
 
A background process that attempts to read from or write to its [[controlling terminal]] is sent signal SIGTTIN (for input) or SIGTTOU (for output). These signals stop the process by default, but they may also be handled in other ways. Shells often override the default stop action of SIGTTOU so that background processes deliver their output to the controlling terminal by default.