Foreground and background jobs
Quick answer
A shell job is a command or pipeline tracked by that shell. Background execution lets a shell continue; wait retrieves the result of its child.
Prepare the practice environment
Use an ordinary user in an authorized disposable Linux VM. From the extracted package root run python3 lab-fixtures/processes/setup.py, enter its printed path and check pwd. Run each executable example in a fresh Bash process with LC_ALL=C and set -o pipefail. Do not use sudo. For interactive examples use a fresh interactive Bash and enter one command at a time. Signal only the just-started bounded child, never an arbitrary PID. Wait for completion before discarding the private workspace. Skip absent tools.
How it works
Interactive Bash job control associates jobs with process groups and a controlling terminal. Only the foreground group normally receives terminal-generated signals such as Ctrl-C (SIGINT) and Ctrl-Z (SIGTSTP). jobs lists jobs known to this shell; %1 is a job specification, not PID 1. bg resumes a stopped job in the background; fg transfers a job to the foreground. In scripts, job control is usually disabled; use a captured PID and wait instead. Background does not mean daemon or parallel speedup. Avoid multiple tasks writing the same file without coordination.
The message started means the shell launched asynchronous work; it does not prove that work succeeded. The example that exits with 7 demonstrates why the status of wait must be inspected separately from the status of the launch. In an interactive shell, a job can consist of a pipeline of several processes, so a job number need not name one process. Only use the interactive example in a fresh terminal as described; in a noninteractive script rely on captured child identities and explicit completion checks.
Try the examples
sleep 0.1 &child=$!printf 'started\n'wait "$child" && printf 'finished\n'The shell continues after &, then waits explicitly.
Expected stdout:
startedfinishedbash -c 'exit 7' &child=$!if wait "$child"; then printf 'ok\n'; else printf 'status=%s\n' "$?"; fiThe background launch status is not the eventual child status.
Expected stdout:
status=7sleep 15# Press Ctrl-Z while sleep runs, then type jobs.# In a fresh interactive shell with only this job, type bg %1, then fg %1.# Let it finish or press Ctrl-C while it is foreground.Use a new interactive Bash with job control on and no other jobs. Enter commands separately, not as a script. Verify the job number before typing bg or fg.
Manual example; not executed locally. Follow its stated environment restrictions.
Common mistakes
Do not run fg/bg in ordinary noninteractive scripts or assume %1 always identifies your job. Ctrl-C targets the terminal foreground group, not every background task.
Practice
In a fresh interactive Bash, stop a short foreground sleep, inspect jobs, resume it with bg and return it to foreground with fg. Let it finish. Then explain how a script would use wait instead.
Related references
Study: jobs, fg, bg, wait, sleep. These are reference names, not deployed links.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Foreground and background jobs — Knowledge Check
Apply process lifecycle and job-control concepts to a bounded child-process lab.
What is %1?
What does bg do?
How do you collect the status of a known child?
What normally receives Ctrl-C?