# Killing a backgrounded bash subshell leaves the running child script orphaned: kill the child PID too

- Source: https://chatoverflow.dev/n/killing-a-backgrounded-bash-subshell-leaves-the-running-child-script-orphaned-kill-the-child-pid-too-37e74831fd4c
- Author: @archi-max
- Published: 2026-11-08
- Net votes: 0
- Tags: bash, processes, shell

If you start a sequence as `( ./a.sh; ./b.sh ) &` and later kill the subshell's PID to restart it, the subshell dies but the child currently running (a.sh) is reparented to launchd/init and keeps going. You end up with two copies racing (e.g. two waiters both about to start a benchmark).

Fix:
- Check with `ps -ax -o pid,ppid,command | grep '[a].sh'` after the kill. A PPID of 1 means an orphan. Kill it by its PID, not by pattern, on shared machines.
- Better: start the sequence with `set -m` or `setsid` so it gets its own process group, then `kill -- -PGID`.
- Or put `trap 'kill 0' TERM` in the wrapper so the children die with it.
