Content deleted Content added
Stevebroshar (talk | contribs) →Job ID: Edit for clarity |
|||
(13 intermediate revisions by one other user not shown) | |||
Line 1:
{{Short description |Control of jobs
{{About |job control on a Unix-based system |the general computing term |job control (computing){{!}}job control}}
In a [[Unix]] or [[Unix-like]] [[operating system]], '''job control''' refers to controlling a [[process group]] as a [[job (computing)|job]] via a [[Unix shell| shell]].<ref>IEEE Std 1003.1-2001, [http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap03.html#tag_03_201 Section 3.201, Job]</ref> Control features include suspend, resume, and terminate, and more advanced features can be performed by sending a [[signal (computing)|signal]] to a job. Job control allows a user to manage processing in the Unix-based [[multiprocessing]] environment, and is distinct from [[job control (computing)|general computing job control]].
Line 23:
==Job ID{{anchor |job ID}}==
A job is identified by a numeric ''job ID'', a.k.a. ''job number'' which is classified as a [[Handle (computing)|handle]] since it is an abstract reference to a [[computer resource |resource]] (a process group). An ID value, prefixed with {{code|%}}, can be used with a job control command to specify a job. The special references {{code|%%}} and {{code|%+}} refer to the default job; the one that would be selected if none specified.<ref>IEEE Std 1003.1-2001, [http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap03.html#tag_03_203 Section 3.203, Job Control Job ID]</ref> Bash documentation refers to a reference (starting with {{code|%}}) as a ''jobspec'' (short for job specification).<ref>[https://www.gnu.org/software/bash/manual/html_node/Job-Control-Basics.html#Job-Control-Basics 7.1 Job Control Basics]</ref>
Job control ID values are typically only used in an interactive shell. In scripting, PGID values are used instead, as they are more precise and robust, and indeed job control is disabled by default in a bash script.
==Foreground/background==
By default, a job runs in the foreground where it uses [[standard streams |interactive input and output]]. The user enters a command line and interacts with the processes
==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]].▼
[[POSIX]] specifies the interface to job control {{endash}} modeled on the Korn shell.<ref>{{man|cu|bg|SUS}}; {{man|cu|fg|SUS}}.</ref>. If a command line ends with {{code|&}}, then the job starts in the background. 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 for the specified background jobs to 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> The foreground job can be paused by pressing {{keypress|Control|Z}} and when paused it can be resumed in the background via command {{code|bg}} (short for background).▼
; Start in background
▲The commands are typically implemented as [[shell builtin]]s; not separate [[computer program |programs]].
: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 |Ctrl| 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) moves background job 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 |wait}}
▲
; Command {{code |bg}} {{anchor| 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).
==
{{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 [[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.
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
In
<!--
==See also==
-->▼
==Notes==
{{Notelist}}
▲-->
==References==
|