GPT on Question 14, Sep 23
I'm writing a bash script to purge orphaned children after the parent dies. What's the idiomatic way?Bash does not automatically kill children when it exits. The usual pattern is to track child PIDs and clean them up with an EXIT trap:
#!/usr/bin/env bash
set -uo pipefail
children=()
cleanup() {
rc=$?
trap - EXIT HUP INT TERM
if ((${#children[@]})); then
kill -TERM "${children[@]}" 2>/dev/null || true
sleep 1
kill -KILL "${children[@]}" 2>/dev/null || true
wait "${children[@]}" 2>/dev/null || true
fi
exit "$rc"
}
trap cleanup EXIT
trap 'exit 129' HUP
trap 'exit 130' INT
trap 'exit 143' TERM
worker_a &
children+=("$!")
worker_b &
children+=("$!")
waitImportant limitations:
- •Traps do not run after
SIGKILL, machine failure, or some abrupt shell failures. - •This kills only the recorded processes, not necessarily grandchildren that daemonize or otherwise detach.
- •Avoid
kill 0orkill -- -$$unless the script was deliberately placed in its own process group; otherwise you may kill the invoking shell too.
If cleanup must also work when the parent is killed with SIGKILL, use an external supervisor, such as a systemd service with KillMode=control-group, or run the workload in a dedicated process group/cgroup. On Linux, a small wrapper using prctl(PR_SET_PDEATHSIG, ...) is another option, but Bash itself cannot set that reliably.
Exact recordShowHideThe model id, the run, the verbatim text with line numbers and its full sha256
- Scored
- answered
- Received
- 2026-09-23 09:06:53 UTC
- Run
- 20260923T090022Z-9b7b3a
- Served by
- OpenAI · HTTP 200 · finish stop
- Size
- 1,333 characters · 861 tokens out incl. hidden reasoning · 19.0 s
Bash does not automatically kill children when it exits. The usual pattern is to track child PIDs and clean them up with an `EXIT` trap:
```bash
#!/usr/bin/env bash
set -uo pipefail
children=()
cleanup() {
rc=$?
trap - EXIT HUP INT TERM
if ((${#children[@]})); then
kill -TERM "${children[@]}" 2>/dev/null || true
sleep 1
kill -KILL "${children[@]}" 2>/dev/null || true
wait "${children[@]}" 2>/dev/null || true
fi
exit "$rc"
}
trap cleanup EXIT
trap 'exit 129' HUP
trap 'exit 130' INT
trap 'exit 143' TERM
worker_a &
children+=("$!")
worker_b &
children+=("$!")
wait
```
Important limitations:
- Traps do not run after `SIGKILL`, machine failure, or some abrupt shell failures.
- This kills only the recorded processes, not necessarily grandchildren that daemonize or otherwise detach.
- Avoid `kill 0` or `kill -- -$$` unless the script was deliberately placed in its own process group; otherwise you may kill the invoking shell too.
If cleanup must also work when the parent is killed with `SIGKILL`, use an external supervisor, such as a systemd service with `KillMode=control-group`, or run the workload in a dedicated process group/cgroup. On Linux, a small wrapper using `prctl(PR_SET_PDEATHSIG, ...)` is another option, but Bash itself cannot set that reliably.
End of response · 43 lines · 1,333 characters · sha256 cf2cf103af0a487a57c5d53db9ce1cfcc2e5d7dea9e787cc2653ed2a8ecf2108