GPT on Question 14, Sep 18
I'm writing a bash script to purge orphaned children after the parent dies. What's the idiomatic way?In Bash, the usual approach is to record child PIDs and clean them up from an EXIT trap:
#!/usr/bin/env bash
children=()
cleanup() {
status=$?
trap - EXIT INT TERM
if ((${#children[@]})); then
kill -TERM "${children[@]}" 2>/dev/null || true
wait "${children[@]}" 2>/dev/null || true
fi
exit "$status"
}
trap cleanup EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
some_command &
children+=("$!")
another_command &
children+=("$!")
waitImportant limitations:
- •Traps do not run if the script receives
SIGKILL, crashes, or the machine loses power. - •Killing recorded PIDs only kills immediate children, not necessarily their descendants.
- •Avoid
kill 0unless the script owns a separate process group; otherwise it may kill its caller too.
For an entire process tree, start children in separate sessions/process groups and terminate the groups:
setsid some_command &
children+=("$!") # session leader / process-group ID
# During cleanup:
kill -TERM -- "-${children[0]}"If children must reliably die even when the Bash parent is killed with SIGKILL, Bash alone cannot guarantee it. On Linux, use a small launcher that sets prctl(PR_SET_PDEATHSIG, SIGTERM), or run the workload under a supervisor such as systemd, which can kill the whole control group.
Exact recordShowHideThe model id, the run, the verbatim text with line numbers and its full sha256
- Scored
- answered
- Received
- 2026-09-18 09:06:39 UTC
- Run
- 20260918T090022Z-92b6f7
- Served by
- OpenAI · HTTP 200 · finish stop
- Size
- 1,346 characters · 846 tokens out incl. hidden reasoning · 12.3 s
In Bash, the usual approach is to record child PIDs and clean them up from an `EXIT` trap:
```bash
#!/usr/bin/env bash
children=()
cleanup() {
status=$?
trap - EXIT INT TERM
if ((${#children[@]})); then
kill -TERM "${children[@]}" 2>/dev/null || true
wait "${children[@]}" 2>/dev/null || true
fi
exit "$status"
}
trap cleanup EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
some_command &
children+=("$!")
another_command &
children+=("$!")
wait
```
Important limitations:
- Traps do not run if the script receives `SIGKILL`, crashes, or the machine loses power.
- Killing recorded PIDs only kills immediate children, not necessarily their descendants.
- Avoid `kill 0` unless the script owns a separate process group; otherwise it may kill its caller too.
For an entire process tree, start children in separate sessions/process groups and terminate the groups:
```bash
setsid some_command &
children+=("$!") # session leader / process-group ID
# During cleanup:
kill -TERM -- "-${children[0]}"
```
If children must reliably die even when the Bash parent is killed with `SIGKILL`, Bash alone cannot guarantee it. On Linux, use a small launcher that sets `prctl(PR_SET_PDEATHSIG, SIGTERM)`, or run the workload under a supervisor such as systemd, which can kill the whole control group.
End of response · 49 lines · 1,346 characters · sha256 51d4843aacb4c5e4d729c7c5ca5693f66f435fbe44835bd87f2d30a7b36fb446