GPT on Question 14, Sep 17
I'm writing a bash script to purge orphaned children after the parent dies. What's the idiomatic way?Bash can reliably clean up children on normal exit or catchable signals by trapping and tracking PIDs:
#!/usr/bin/env bash
set -Eeuo pipefail
children=()
cleanup() {
local status=$?
trap - EXIT INT TERM HUP
if ((${#children[@]})); then
kill -TERM "${children[@]}" 2>/dev/null || true
wait "${children[@]}" 2>/dev/null || true
fi
exit "$status"
}
trap cleanup EXIT INT TERM HUP
worker1 & children+=("$!")
worker2 & children+=("$!")
waitThis does not run if the parent receives SIGKILL, crashes, or the machine loses power. There is no fully reliable portable Bash mechanism for that.
If descendants can spawn their own children, use OS-level supervision rather than recursively searching by PPID:
- •systemd: put the script in a service/cgroup and use
KillMode=control-group. - •Linux child wrapper: set
prctl(PR_SET_PDEATHSIG, SIGTERM)beforeexec(not directly available in Bash). - •Dedicated process group/session: possible with
setsid, but avoid blindly usingkill 0orkill -- -$$unless you know the script owns that process group, since it may kill unrelated processes or itself prematurely.
For ordinary scripts, the PID-array plus trap pattern is idiomatic; for guaranteed cleanup after abrupt parent death, use a supervisor/cgroup.
Exact recordShowHideThe model id, the run, the verbatim text with line numbers and its full sha256
- Scored
- answered
- Received
- 2026-09-17 09:06:50 UTC
- Run
- 20260917T090022Z-136fc3
- Served by
- OpenAI · HTTP 200 · finish stop
- Size
- 1,331 characters · 841 tokens out incl. hidden reasoning · 17.6 s
Bash can reliably clean up children on normal exit or catchable signals by trapping and tracking PIDs:
```bash
#!/usr/bin/env bash
set -Eeuo pipefail
children=()
cleanup() {
local status=$?
trap - EXIT INT TERM HUP
if ((${#children[@]})); then
kill -TERM "${children[@]}" 2>/dev/null || true
wait "${children[@]}" 2>/dev/null || true
fi
exit "$status"
}
trap cleanup EXIT INT TERM HUP
worker1 & children+=("$!")
worker2 & children+=("$!")
wait
```
This does **not** run if the parent receives `SIGKILL`, crashes, or the machine loses power. There is no fully reliable portable Bash mechanism for that.
If descendants can spawn their own children, use OS-level supervision rather than recursively searching by PPID:
- **systemd:** put the script in a service/cgroup and use `KillMode=control-group`.
- **Linux child wrapper:** set `prctl(PR_SET_PDEATHSIG, SIGTERM)` before `exec` (not directly available in Bash).
- **Dedicated process group/session:** possible with `setsid`, but avoid blindly using `kill 0` or `kill -- -$$` unless you know the script owns that process group, since it may kill unrelated processes or itself prematurely.
For ordinary scripts, the PID-array plus `trap` pattern is idiomatic; for guaranteed cleanup after abrupt parent death, use a supervisor/cgroup.
End of response · 37 lines · 1,331 characters · sha256 09fda7b6e8d0802d3408c89c2e40988f38c3fe4502c9db24ac583998149d5bf7