Signals, process groups and forwarding
A signal is a notification to a process. TERM requests termination and may be handled; KILL cannot be caught or ignored. kill sends a signal rather than necessarily killing immediately. A process group may contain several processes; signaling a group has a wider effect than signaling one PID. Never send a signal to a guessed PID, zero, a negative group identifier or an unrelated process in these labs. Track children you created and avoid stale PIDs: the OS can reuse a numeric PID after termination, and a check-then-signal sequence is not race-free. kill -0 performs an existence/permission check, not an identity guarantee. Use an owned worker, readiness handshake, bounded lifetime and explicit wait. A production supervisor may need process-group containment and robust identity mechanisms beyond Bash. Traps in a waiting shell may interrupt wait before a child actually ends; follow up with the appropriate lifecycle check. A successful signal send only confirms that the request was accepted. In the fixed worker example, the expected status 9 comes from its trap, not from a universal mapping of TERM to exit status.
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.
Handle a signal in a child
bash -c 'trap "exit 7" TERM; kill -TERM "$BASHPID"; exit 1'printf 'status=%s\n' "$?"Expected stdout:
status=7The child signals only itself and its handler chooses status 7.
Check existence of self
Expected stdout:
self existsSignal zero does not deliver a signal. It is not a general identity proof.
Forward TERM to a bounded worker
coproc WORKER { exec bash -c 'trap "exit 9" TERM; printf "ready\n"; read -r -t 3 ignored; exit 4'; }pid=${WORKER_PID-}fd=${WORKER[0]-}[[ -n $pid && -n $fd ]] || exit 1if IFS= read -r -t 1 -u "$fd" reply && [[ $reply == ready ]]; then kill -TERM "$pid" || { wait "$pid"; exit 1; }else wait "$pid" exit 1fiwait "$pid"printf 'worker=%s\n' "$?"Expected stdout:
worker=9The worker is a known child with a readiness message and a finite read deadline. This is not a race-free PID supervision method.
Common mistakes
Do not use kill -0 to prove ownership. Do not signal a previously reaped PID. Do not blindly forward signals to entire groups or assume a trap always runs.
Practice
Adapt the bounded worker example to return 9 from its TERM handler. Check the readiness reply before sending TERM, collect the resulting status, and explain why this controlled test does not solve general PID reuse.
Next: Bounded worker pools