Gemini on Question 14, Oct 2
I'm writing a bash script to purge orphaned children after the parent dies. What's the idiomatic way?The most idiomatic and robust way to handle this in Bash is to use the trap command.
Depending on whether your script spawns just direct children or deeply nested grandchild processes, there are two standard approaches.
Method 1: The jobs -p approach (Best for direct children)
If your script simply spawns background tasks using &, the safest approach is to trap the EXIT signal and kill all active jobs.
#!/bin/bash
# Define the cleanup function
cleanup() {
# Suppress error messages if processes are already dead
kill $(jobs -p) 2>/dev/null
}
# Trap EXIT (normal termination), SIGINT (Ctrl+C), and SIGTERM (kill)
trap cleanup EXIT INT TERM
# --- Your script logic below ---
echo "Parent started. Spawning children..."
sleep 100 &
sleep 200 &
# Wait for children to finish (or for a signal to interrupt the script)
waitWhy this is good:
It strictly targets only the background jobs spawned by this specific script. It won't accidentally kill unrelated processes.
Method 2: The Process Group approach (Best for nested children)
If your child processes spawn their own child processes (grandchildren to your script), jobs -p won't catch them. To kill the entire process tree, you send a signal to the process group using kill 0.
#!/bin/bash # Send SIGTERM to all processes in the current process group trap 'trap - EXIT; kill 0' EXIT INT TERM # --- Your script logic below --- echo "Parent started. Spawning nested children..." # A child that spawns a grandchild ( sleep 100 ; sleep 200 ) & wait
How this works:kill 0 sends a SIGTERM to every process in the script's process group.
Note on trap - EXIT: We remove the trap right before killing the group so the script doesn't get caught in an infinite loop of trying to kill itself.
⚠️ The kill -9 (SIGKILL) Caveat
No bash script can catch SIGKILL. If someone runs kill -9 <parent_pid>, the trap will not execute, and you will end up with orphaned children.
If you absolutely must guarantee that children die even if the parent is kill -9'd, you have to move outside pure Bash. You have two main options:
- 1.Use
systemd: Run your script as asystemdservice. By default, when a systemd service is stopped or killed, systemd usescgroupsto flawlessly hunt down and kill every single child and grandchild process. - 2.Use Linux
prctl(via a wrapper): You can use a tool likepdeathsigor a tiny python wrapper to utilizePR_SET_PDEATHSIG. This tells the Linux kernel itself to send a signal to the child the exact microsecond the parent dies.
For 99% of bash scripts, however, Method 1 (the trap with jobs -p) is the accepted idiom.
Exact recordShowHideThe model id, the run, the verbatim text with line numbers and its full sha256
- Scored
- answered
- Received
- 2026-10-02 09:07:06 UTC
- Run
- 20261002T090008Z-e178a0
- Served by
- Google · HTTP 200 · finish stop
- Size
- 2,744 characters · 2,242 tokens out incl. hidden reasoning · 21.0 s
End of response · 68 lines · 2,744 characters · sha256 24599c51b0eb8778f8b245c4dbd8384903ddf2184da2fe87b97c7e82c6923e30