Created
September 26, 2012 05:56
-
-
Save criccomini/3786342 to your computer and use it in GitHub Desktop.
Killing Subprocesses in Linux/Bash - Recursive Trap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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