Skip to content

Instantly share code, notes, and snippets.

@criccomini
Created September 26, 2012 05:56
Show Gist options
  • Select an option

  • Save criccomini/3786342 to your computer and use it in GitHub Desktop.

Select an option

Save criccomini/3786342 to your computer and use it in GitHub Desktop.
Killing Subprocesses in Linux/Bash - Recursive Trap
kill_child_processes() {
isTopmost=$1
curPid=$2
childPids=`ps -o pid --no-headers --ppid ${curPid}`
for childPid in $childPids
do
kill_child_processes 0 $childPid
done
if [ $isTopmost -eq 0 ]; then
kill -9 $curPid 2> /dev/null
fi
}
# Ctrl-C trap. Catches INT signal
trap "kill_child_processes 1 $$; exit 0" INT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment