Job control (Unix): Difference between revisions

Content deleted Content added
Overview: use job vs process more consistently
Move up what a job is
Line 2:
{{About |job control on a Unix 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]]. 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]].
 
A job mapsencompasses toall aof singlethe processes that start for the handling of a shell command executionline. whichA maysimple resultcommand inline multiplemy processesstart startingjust andone completing. Multi-process, operationsbut comea aboutcommand becauseline may result in multiple processes since a process maycan create child processes, and a single shell command mayline consistcan ofspecify a [[Pipeline (Unix)|pipeline]] of multiple communicating processescommands. For example, the following command line selects lines containing the text "title", sorts them alphabetically, and displays the result in a [[terminal pager]]: <code>grep title somefile.txt | sort | less</code>. This creates at least three processes: one for [[grep|{{code |grep}}]], one for {{code |sort}}, and one for [[less (Unix)|{{code |less}}]]. Job control allows the shell to control these related processes as one entity, and when a user issues the appropriate key combination (usually {{keypress |Control|Z}}), the group of processes is suspended.
 
[[POSIX]] specifies job control commands {{endash}} modeled after the Korn shell job control commands.<ref>{{man|cu|bg|SUS}}; {{man|cu|fg|SUS}}.</ref>. Command {{code|bg}} resumes the foreground job paused via {{keypress|Control|Z}} as a background job. Command {{code|fg}} causes the background job to run in the foreground; either the job specified or the one most recently sent 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 is 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>
Line 25 ⟶ 27:
 
However, a user may wish to perform an operation while using the shell for another purpose. An operation that is running but not using the interactive input and output is running in the background. The single operation that is using the interactive input and output is running in the foreground. Job control is the facility to control how operations run as foreground or background. A user can start a job in the background, send the foreground job to the background, bring a background job to the foreground, and suspend or terminate a job.
 
A job maps to a single shell command execution which may result in multiple processes starting and completing. Multi-process operations come about because a process may create child processes, and a single shell command may consist of a [[Pipeline (Unix)|pipeline]] of multiple communicating processes. For example, the following command line selects lines containing the text "title", sorts them alphabetically, and displays the result in a [[terminal pager]]: <code>grep title somefile.txt | sort | less</code>. This creates at least three processes: one for [[grep|{{code |grep}}]], one for {{code |sort}}, and one for [[less (Unix)|{{code |less}}]]. Job control allows the shell to control these related processes as one entity, and when a user issues the appropriate key combination (usually {{keypress |Control|Z}}), the group of processes is suspended.
 
As defined by [[POSIX]], a job is managed by the operating system as a [[process group]].<ref>IEEE Std 1003.1-2001, [http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap03.html#tag_03_201 Section 3.201, Job]</ref>