summaryrefslogtreecommitdiff
path: root/devdocs/bash/job-control-basics.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/bash/job-control-basics.html
new repository
Diffstat (limited to 'devdocs/bash/job-control-basics.html')
-rw-r--r--devdocs/bash/job-control-basics.html9
1 files changed, 9 insertions, 0 deletions
diff --git a/devdocs/bash/job-control-basics.html b/devdocs/bash/job-control-basics.html
new file mode 100644
index 00000000..497d3bb8
--- /dev/null
+++ b/devdocs/bash/job-control-basics.html
@@ -0,0 +1,9 @@
+<h1 class="section">Job Control Basics</h1> <p>Job control refers to the ability to selectively stop (suspend) the execution of processes and continue (resume) their execution at a later point. A user typically employs this facility via an interactive interface supplied jointly by the operating system kernel’s terminal driver and Bash. </p> <p>The shell associates a <var>job</var> with each pipeline. It keeps a table of currently executing jobs, which may be listed with the <code>jobs</code> command. When Bash starts a job asynchronously, it prints a line that looks like: </p>
+<div class="example"> <pre class="example">[1] 25647
+</pre>
+</div> <p>indicating that this job is job number 1 and that the process <small>ID</small> of the last process in the pipeline associated with this job is 25647. All of the processes in a single pipeline are members of the same job. Bash uses the <var>job</var> abstraction as the basis for job control. </p> <p>To facilitate the implementation of the user interface to job control, the operating system maintains the notion of a current terminal process group <small>ID</small>. Members of this process group (processes whose process group <small>ID</small> is equal to the current terminal process group <small>ID</small>) receive keyboard-generated signals such as <code>SIGINT</code>. These processes are said to be in the foreground. Background processes are those whose process group <small>ID</small> differs from the terminal’s; such processes are immune to keyboard-generated signals. Only foreground processes are allowed to read from or, if the user so specifies with <code>stty tostop</code>, write to the terminal. Background processes which attempt to read from (write to when <code>stty tostop</code> is in effect) the terminal are sent a <code>SIGTTIN</code> (<code>SIGTTOU</code>) signal by the kernel’s terminal driver, which, unless caught, suspends the process. </p> <p>If the operating system on which Bash is running supports job control, Bash contains facilities to use it. Typing the <em>suspend</em> character (typically ‘<samp>^Z</samp>’, Control-Z) while a process is running causes that process to be stopped and returns control to Bash. Typing the <em>delayed suspend</em> character (typically ‘<samp>^Y</samp>’, Control-Y) causes the process to be stopped when it attempts to read input from the terminal, and control to be returned to Bash. The user then manipulates the state of this job, using the <code>bg</code> command to continue it in the background, the <code>fg</code> command to continue it in the foreground, or the <code>kill</code> command to kill it. A ‘<samp>^Z</samp>’ takes effect immediately, and has the additional side effect of causing pending output and typeahead to be discarded. </p> <p>There are a number of ways to refer to a job in the shell. The character ‘<samp>%</samp>’ introduces a job specification (<em>jobspec</em>). </p> <p>Job number <code>n</code> may be referred to as ‘<samp>%n</samp>’. The symbols ‘<samp>%%</samp>’ and ‘<samp>%+</samp>’ refer to the shell’s notion of the current job, which is the last job stopped while it was in the foreground or started in the background. A single ‘<samp>%</samp>’ (with no accompanying job specification) also refers to the current job. The previous job may be referenced using ‘<samp>%-</samp>’. If there is only a single job, ‘<samp>%+</samp>’ and ‘<samp>%-</samp>’ can both be used to refer to that job. In output pertaining to jobs (e.g., the output of the <code>jobs</code> command), the current job is always flagged with a ‘<samp>+</samp>’, and the previous job with a ‘<samp>-</samp>’. </p> <p>A job may also be referred to using a prefix of the name used to start it, or using a substring that appears in its command line. For example, ‘<samp>%ce</samp>’ refers to a stopped job whose command name begins with ‘<samp>ce</samp>’. Using ‘<samp>%?ce</samp>’, on the other hand, refers to any job containing the string ‘<samp>ce</samp>’ in its command line. If the prefix or substring matches more than one job, Bash reports an error. </p> <p>Simply naming a job can be used to bring it into the foreground: ‘<samp>%1</samp>’ is a synonym for ‘<samp>fg %1</samp>’, bringing job 1 from the background into the foreground. Similarly, ‘<samp>%1 &amp;</samp>’ resumes job 1 in the background, equivalent to ‘<samp>bg %1</samp>’ </p> <p>The shell learns immediately whenever a job changes state. Normally, Bash waits until it is about to print a prompt before reporting changes in a job’s status so as to not interrupt any other output. If the <samp>-b</samp> option to the <code>set</code> builtin is enabled, Bash reports such changes immediately (see <a href="the-set-builtin">The Set Builtin</a>). Any trap on <code>SIGCHLD</code> is executed for each child process that exits. </p> <p>If an attempt to exit Bash is made while jobs are stopped, (or running, if the <code>checkjobs</code> option is enabled – see <a href="the-shopt-builtin">The Shopt Builtin</a>), the shell prints a warning message, and if the <code>checkjobs</code> option is enabled, lists the jobs and their statuses. The <code>jobs</code> command may then be used to inspect their status. If a second attempt to exit is made without an intervening command, Bash does not print another warning, and any stopped jobs are terminated. </p> <p>When the shell is waiting for a job or process using the <code>wait</code> builtin, and job control is enabled, <code>wait</code> will return when the job changes state. The <samp>-f</samp> option causes <code>wait</code> to wait until the job or process terminates before returning. </p><div class="_attribution">
+ <p class="_attribution-p">
+ Copyright &copy; 2000, 2001, 2002, 2007, 2008 Free Software Foundation, Inc.<br>Licensed under the GNU Free Documentation License.<br>
+ <a href="https://www.gnu.org/software/bash/manual/html_node/Job-Control-Basics.html" class="_attribution-link">https://www.gnu.org/software/bash/manual/html_node/Job-Control-Basics.html</a>
+ </p>
+</div>