GPT on Question 14, Sep 22
I'm writing a bash script to purge orphaned children after the parent dies. What's the idiomatic way?In Bash, the idiomatic approach is to trap script termination, kill tracked children, and then reap them:
#!/usr/bin/env bash
children=()
cleanup() {
trap - EXIT INT TERM HUP
if ((${#children[@]})); then
kill "${children[@]}" 2>/dev/null || :
wait "${children[@]}" 2>/dev/null || :
fi
}
trap cleanup EXIT
trap 'exit 129' HUP
trap 'exit 130' INT
trap 'exit 143' TERM
some_command &
children+=("$!")
another_command &
children+=("$!")
waitImportant limitations:
- •
EXITtraps do not run afterSIGKILL, a kernel crash, or abrupt power loss. - •This kills only the recorded direct children, not arbitrary grandchildren.
- •Avoid
kill 0unless the script runs in its own process group; otherwise it may kill the invoking shell or unrelated processes.
For reliable cleanup of an entire process tree, use an external supervisor or containment mechanism:
- •A
systemdservice withKillMode=control-group - •A dedicated cgroup/container
- •On Linux, a child wrapper using
prctl(PR_SET_PDEATHSIG, SIGTERM) - •A dedicated process group/session, killed as a group
Once the parent is already dead, Bash itself cannot perform cleanup; orphaned processes are reparented. Cleanup must therefore be arranged beforehand or handled by a supervisor.
Exact recordShowHideThe model id, the run, the verbatim text with line numbers and its full sha256
- Scored
- answered
- Received
- 2026-09-22 09:06:39 UTC
- Run
- 20260922T090022Z-7ff13b
- Served by
- OpenAI · HTTP 200 · finish stop
- Size
- 1,288 characters · 1,346 tokens out incl. hidden reasoning · 27.2 s
In Bash, the idiomatic approach is to trap script termination, kill tracked children, and then reap them:
```bash
#!/usr/bin/env bash
children=()
cleanup() {
trap - EXIT INT TERM HUP
if ((${#children[@]})); then
kill "${children[@]}" 2>/dev/null || :
wait "${children[@]}" 2>/dev/null || :
fi
}
trap cleanup EXIT
trap 'exit 129' HUP
trap 'exit 130' INT
trap 'exit 143' TERM
some_command &
children+=("$!")
another_command &
children+=("$!")
wait
```
Important limitations:
- `EXIT` traps do not run after `SIGKILL`, a kernel crash, or abrupt power loss.
- This kills only the recorded direct children, not arbitrary grandchildren.
- Avoid `kill 0` unless the script runs in its own process group; otherwise it may kill the invoking shell or unrelated processes.
For reliable cleanup of an entire process tree, use an external supervisor or containment mechanism:
- A `systemd` service with `KillMode=control-group`
- A dedicated cgroup/container
- On Linux, a child wrapper using `prctl(PR_SET_PDEATHSIG, SIGTERM)`
- A dedicated process group/session, killed as a group
Once the parent is already dead, Bash itself cannot perform cleanup; orphaned processes are reparented. Cleanup must therefore be arranged beforehand or handled by a supervisor.
End of response · 44 lines · 1,288 characters · sha256 c4b1d2bc29704f7c4cfef9244530fd2c05d35fee569ad9d37df115daa14069ac