Interactive job control
A job is Bash’s unit of job control, often a pipeline with multiple processes. A PID identifies one process; a jobspec such as %1 names a job in the current shell’s job table, not a system-wide identifier. In an interactive terminal, Ctrl-Z commonly stops the foreground job; jobs lists jobs, bg %n resumes a stopped job in the background, and fg %n brings it to the foreground. Substitute the actual number shown by jobs. The terminal foreground process group receives keyboard-generated signals. Noninteractive shells usually disable job control; set -m enables monitor mode, but this alone does not provide a controlling terminal. jobs -p reports job process-group leader IDs; disown -h marks a job so Bash will not send it SIGHUP when Bash receives SIGHUP. This is not a service manager or guarantee of survival. In an interactive disposable shell only, start sleep 10, press Ctrl-Z promptly, inspect jobs, resume it using bg, then fg and allow completion. Do not apply these commands to existing work. That terminal lab is manual, not verified here. For the manual terminal lab, use a fresh shell and only the job that you started. The short automated jobs example is timing-sensitive under heavy scheduling delays.
Practical examples
Run each example as a separate script in a fresh noninteractive Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code.
Inspect a known job
sleep 0.3 &pid=$!jobs -p > jobs.txt || { wait "$pid"; exit 1; }if grep -Fxq "$pid" jobs.txt; then printf 'job listed\n'else exit 1fiwait "$pid"Expected stdout:
job listedThis checks a simple one-process background job in a noninteractive shell; the PID varies.
Inspect monitor mode
set +m[[ $- != *m* ]] && printf 'monitor off\n'set -m || exit 1[[ $- == *m* ]] && printf 'monitor on\n'set +mExpected stdout:
monitor offmonitor onThe m flag shows monitor mode; no controlling terminal is inferred.
Mark a known job
sleep 0.2 &pid=$!disown -h "$pid" || exit 1wait "$pid" || exit 1printf 'marked and collected\n' Expected stdout:
marked and collectedThe mark retains the job in the table; this does not test actual hangup delivery.
Common mistakes
Never confuse %1 with PID 1. Do not assume a script can use fg or bg without job control. Do not use disown as a substitute for supervision and log management.
Practice
In a new interactive Bash with no existing jobs, perform the bounded sleep exercise described above. Record the observed jobs output, distinguish job ID and PID, and explain why the result depends on the terminal. If a terminal is unavailable, explain the steps without claiming execution.
Next: Signals, process groups and forwarding