Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Kill process tree - shell function and script.
#!/bin/sh
KILLSIG=""
walktree() { local p=""; for p in "$@"; do walktree $(pgrep -P "$p"); echo $p; done; }
test "${1#-}" != "$1" && KILLSIG="$1" && shift
test $# = "0" && echo "Usage: ${0##*/} pid ..." && exit
PIDS="$(walktree "$@")"
while kill $KILLSIG $PIDS; do sleep 0.2; done
exit 0
## END ##
#!/bin/sh
killtree() {
local KILLSIG=""
test "${1#-}" = "$1" || { KILLSIG="$1"; shift; }
test $# = "0" && echo "Usage: ${0##*/} pid ..." && return 1
local p=""; for p in "$@"; do killtree $(pgrep -P "$p"); kill $KILLSIG "$p"; done;
}
killtree "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment