Job Control in Bash¶
Job control is a feature of Bash that allows users to selectively stop (suspend) the execution of processes and continue (resume) their execution at a later point.
This mechanism is particularly useful for managing multiple processes from a single bash session, i.e., a single shell.
Table of Contents¶
- Key Concepts
- Job Control Commands
- Referring to Jobs
- Job States and Notifications
- Examples
- Exiting Bash with Active Jobs
- Waiting for a Job
- Important Notes
Key Concepts¶
-
Job: A job is associated with each pipeline and can be a single command or a group of commands connected by pipes.
- Bash maintains a table of current jobs which can be displayed using the
jobs
command.
- Bash maintains a table of current jobs which can be displayed using the
-
Process Group: A job might consist of multiple processes, especially if pipelines are used.
- These processes share the same process group ID.
-
Foreground and Background Jobs: Jobs can either run in the foreground, blocking the terminal until completion, or in the background, allowing the terminal to be used for other tasks.
-
Standard File Descriptors (FD) are the primary means of interacting with processes:
- Standard input (stdin - FD 0)
- Standard output (stdout - FD 1)
- Standard error (stderr - FD 2)
Job Control Commands¶
jobs
: Lists all current jobs with their statuses.fg
: Brings a job to the foreground.- Usage:
fg %job_number
orfg %job_name
.
- Usage:
bg
: Continues a job in the background.- Usage:
bg %job_number
orbg %job_name
.
- Usage:
kill
: Sends a signal to a job.- Usage:
kill %job_number
.
- Usage:
ctrl+z
: Suspends the foreground job.ctrl+y
: Delays suspending a job until it attempts to read input.
Referring to Jobs¶
%n
: Refers to job numbern
.%string
: Refers to a job starting withstring
.%?string
: Refers to a job containingstring
.%%
or%+
: Refers to the current job.%-
: Refers to the previous job.
Job States and Notifications¶
- Bash reports a job's state change (e.g., from running to stopped) when it's
about to display the prompt.
- Use the
-b
option with theset
command for immediate notifications.
- Use the
SIGCHLD
: Bash executes any trap on SIGCHLD for each child that exits.
Examples¶
Running a Job in the Background¶
sleep 30 &
sleep
command in the background, allowing the terminal
to be used for other commands.
Suspending a Foreground Job¶
While a job is running in the foreground, pressing <C-z>
will suspend
it, returning control to the shell.
Moving a Job to the Background¶
bg %1
Bringing a Job to the Foreground¶
fg %1
Listing Jobs¶
jobs
Exiting Bash with Active Jobs¶
- Bash warns on exit if jobs are stopped or, if
checkjobs
is enabled, running.- A second exit attempt terminates stopped jobs.
Waiting for a Job¶
- The
wait
builtin waits for a job to change state.- With job control enabled,
wait
returns if the job terminates.
- With job control enabled,
Important Notes¶
Naming a job can be used to bring it into the foreground:
%1
is a synonym forfg %1
, bringing job 1 from the background into the foreground.-
%1 &
resumes job 1 in the background, equivalent tobg %1
. -
Background Processes and Terminal I/O: Background processes attempting to read from or write to the terminal can be suspended by the terminal driver (SIGTTIN or SIGTTOU signals).
- Manipulating File Descriptors: The
exec
command can be used to redirect file descriptors for the current shell session.
Understanding job control enhances multitasking capabilities in Bash, providing flexibility in how processes are managed during a shell session.
This revision provides a comprehensive overview of job control in Bash, including key concepts, commands, how to refer to jobs, handling job states, and practical examples.
It clarifies ambiguous points and adds detailed explanations suitable for learners.