GPT on Question 14, Sep 10
I'm writing a bash script to purge orphaned children after the parent dies. What's the idiomatic way?For children started by the Bash script, the usual pattern is to record their PIDs and clean them up with an EXIT trap:
#!/usr/bin/env bash
children=()
cleanup() {
rc=$?
trap - EXIT INT TERM
if ((${#children[@]})); then
kill -TERM -- "${children[@]}" 2>/dev/null || true
wait "${children[@]}" 2>/dev/null || true
fi
exit "$rc"
}
trap cleanup EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
some_command &
children+=("$!")
another_command &
children+=("$!")
waitImportant limitations:
- •
EXITtraps do not run if the parent receivesSIGKILL, crashes severely, or the machine loses power. - •This only handles recorded direct children, not necessarily grandchildren or processes that daemonize.
- •Avoid blindly using
kill 0orkill -- -$$; those signal an entire process group and may include unrelated processes or the shell itself.
If children must die even when the Bash parent is killed with SIGKILL, use an external mechanism:
- •systemd service/cgroup with
KillMode=control-group—generally the most robust option. - •On Linux, have each child set
prctl(PR_SET_PDEATHSIG, SIGTERM). - •Run the workload in a dedicated process group/session managed by a surviving supervisor.
So: use an EXIT trap for normal script cleanup; use a supervisor/cgroup or parent-death signal for a hard guarantee.
Exact recordShowHideThe model id, the run, the verbatim text with line numbers and its full sha256
- Scored
- answered
- Received
- 2026-09-10 22:15:53 UTC
- Run
- 20260910T221001Z-bb5bd1
- Served by
- OpenAI · HTTP 200 · finish stop
- Size
- 1,375 characters · 1,351 tokens out incl. hidden reasoning · 22.2 s
For children started by the Bash script, the usual pattern is to record their PIDs and clean them up with an `EXIT` trap:
```bash
#!/usr/bin/env bash
children=()
cleanup() {
rc=$?
trap - EXIT INT TERM
if ((${#children[@]})); then
kill -TERM -- "${children[@]}" 2>/dev/null || true
wait "${children[@]}" 2>/dev/null || true
fi
exit "$rc"
}
trap cleanup EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
some_command &
children+=("$!")
another_command &
children+=("$!")
wait
```
Important limitations:
- `EXIT` traps do **not** run if the parent receives `SIGKILL`, crashes severely, or the machine loses power.
- This only handles recorded direct children, not necessarily grandchildren or processes that daemonize.
- Avoid blindly using `kill 0` or `kill -- -$$`; those signal an entire process group and may include unrelated processes or the shell itself.
If children must die even when the Bash parent is killed with `SIGKILL`, use an external mechanism:
- **systemd service/cgroup** with `KillMode=control-group`—generally the most robust option.
- On Linux, have each child set `prctl(PR_SET_PDEATHSIG, SIGTERM)`.
- Run the workload in a dedicated process group/session managed by a surviving supervisor.
So: use an `EXIT` trap for normal script cleanup; use a supervisor/cgroup or parent-death signal for a hard guarantee.
End of response · 45 lines · 1,375 characters · sha256 02b8a054d976d3a0289cd0b9c4036f917bd1fe58987aac1aacef8a873fbba7ff