CodeRigor

FREE LESSON

Background work, wait & cleanup traps

Wait for the child you launched, preserve its status and remove temporary work on the parent’s exit path.

Before this lesson, complete bash-files, bash-pipelines.

This course runs GNU Bash with ordinary GNU text tools in a fresh Linux sandbox.

Appending & launches asynchronous work and lets the shell continue. $! identifies the most recent asynchronous job's process, so save it immediately before launching more work. Starting a child is not the same as collecting its outcome. Use wait with the saved PID when a particular worker's status is part of the parent contract.

Launch and collect
start / 3
Launch and collect

Apply the contract

A bare wait waits for outstanding jobs but does not give the failure result of the specific child this lab needs. if wait "$pid"; then ... else status=$?; ... fi both waits and preserves the result before another command overwrites it. Background functions generally run in a subshell, so assigning a variable there is not a communication mechanism back to the parent. Use explicit output, files or another deliberate IPC interface.

Preserve the failure status
start / 3
Preserve the failure status
Bash
worker() { return 7; }
worker &
pid=$!
if wait "$pid"; then printf 'ok\n'; else result=$?; printf 'child=%s\n' "$result"; fi
trap 'printf "cleanup\n"' EXIT

Reason about the edge cases

An EXIT trap runs the cleanup function when the parent shell exits normally through the supported paths. Install it before work that can leave a temporary file, and ensure cleanup cannot reinterpret its filename as an option. This lab uses a fixed private filename in a fresh workspace and checks its absence after completion. Real scripts should use mktemp for unpredictable temporary names and consider signals and ownership. An EXIT trap cannot promise recovery from SIGKILL or a machine crash. The graded exercise avoids timing assumptions: waiting, reporting status and removing the file are observable outcomes, regardless of scheduling speed.

Temporary file lifecycle
start / 3
Temporary file lifecycle

Practice

Launch a background worker that writes working plus newline to worker.tmp and returns the supplied argument (an integer 0–9). Wait for that specific child. Print ok for status zero, otherwise failed:N. The parent exits successfully in either case and worker.tmp must be absent when it finishes. Use an EXIT cleanup path. No wall-clock timing is graded.

Run sample starts again from the first public fixture and grants no mastery evidence. Submit checks all fixtures, including edge cases. Read the specification before editing; the bug and fill phases share the build’s contract. Flashcards reinforce recall on a separate schedule.

Watch & read

Free lectures, talks and readings that cover this skill from another angle. All resources →

Free shell scripting foundation. Use the authored lesson and GNU Bash manual for this lab’s precise argument, status and process contract.