Skip to content

Instantly share code, notes, and snippets.

@ilyaevseev
Last active October 12, 2020 23:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilyaevseev/902b7c2de6b480cff6c8741cb7ab26d4 to your computer and use it in GitHub Desktop.
Save ilyaevseev/902b7c2de6b480cff6c8741cb7ab26d4 to your computer and use it in GitHub Desktop.
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