GPT on Question 14, Sep 28
I'm writing a bash script to purge orphaned children after the parent dies. What's the idiomatic way?An orphan is not a zombie: once its parent dies, it is reparented, and Bash can no longer reliably identify it by its original parent. The idiomatic Bash approach is therefore to track children and terminate them during parent cleanup:
#!/usr/bin/env bash
set -Eeuo pipefail
children=()
cleanup() {
status=$?
trap - EXIT HUP INT TERM
if ((${#children[@]})); then
kill -TERM "${children[@]}" 2>/dev/null || :
wait "${children[@]}" 2>/dev/null || :
fi
exit "$status"
}
trap cleanup EXIT
trap 'exit 129' HUP
trap 'exit 130' INT
trap 'exit 143' TERM
worker_one &
children+=("$!")
worker_two &
children+=("$!")
waitImportant limitations:
- •Traps do not run if the parent receives
SIGKILL, crashes severely, or the machine loses power. - •Killing direct child PIDs does not necessarily kill grandchildren.
- •Avoid
trap 'kill 0' EXIT; it signals the entire current process group and may kill the invoking shell or unrelated processes.
If cleanup must also work after an untrappable parent death, use an OS-level mechanism rather than pure Bash:
- •Linux: have each child set
prctl(PR_SET_PDEATHSIG, SIGTERM)beforeexec—usually via a small C wrapper or a suitable utility. - •systemd: run the workload as a service/scope with
KillMode=control-group; systemd will clean up the whole cgroup. - •Containers/cgroups: place the workload in its own cgroup and terminate the cgroup.
- •Process groups: useful for descendants, but require an external supervisor to kill the group if the group leader itself dies.
For production supervision, systemd or another process supervisor is generally the most reliable and idiomatic solution.
Exact recordShowHideThe model id, the run, the verbatim text with line numbers and its full sha256
- Scored
- answered
- Received
- 2026-09-28 09:05:52 UTC
- Run
- 20260928T090007Z-07d4be
- Served by
- OpenAI · HTTP 200 · finish stop
- Size
- 1,702 characters · 1,441 tokens out incl. hidden reasoning · 22.5 s
An orphan is not a zombie: once its parent dies, it is reparented, and Bash can no longer reliably identify it by its original parent. The idiomatic Bash approach is therefore to track children and terminate them during parent cleanup:
```bash
#!/usr/bin/env bash
set -Eeuo pipefail
children=()
cleanup() {
status=$?
trap - EXIT HUP INT TERM
if ((${#children[@]})); then
kill -TERM "${children[@]}" 2>/dev/null || :
wait "${children[@]}" 2>/dev/null || :
fi
exit "$status"
}
trap cleanup EXIT
trap 'exit 129' HUP
trap 'exit 130' INT
trap 'exit 143' TERM
worker_one &
children+=("$!")
worker_two &
children+=("$!")
wait
```
Important limitations:
- Traps do not run if the parent receives `SIGKILL`, crashes severely, or the machine loses power.
- Killing direct child PIDs does not necessarily kill grandchildren.
- Avoid `trap 'kill 0' EXIT`; it signals the entire current process group and may kill the invoking shell or unrelated processes.
If cleanup must also work after an untrappable parent death, use an OS-level mechanism rather than pure Bash:
- **Linux:** have each child set `prctl(PR_SET_PDEATHSIG, SIGTERM)` before `exec`—usually via a small C wrapper or a suitable utility.
- **systemd:** run the workload as a service/scope with `KillMode=control-group`; systemd will clean up the whole cgroup.
- **Containers/cgroups:** place the workload in its own cgroup and terminate the cgroup.
- **Process groups:** useful for descendants, but require an external supervisor to kill the group if the group leader itself dies.
For production supervision, systemd or another process supervisor is generally the most reliable and idiomatic solution.
End of response · 48 lines · 1,702 characters · sha256 a3d9646059f9d166be96d0a9f7f05441c0b2149e2bd4b01f6bfda0c75d4cefb7