Last active
June 23, 2020 23:42
-
-
Save tubaman/377458a11fedb49cc2132ea6e31ae8b2 to your computer and use it in GitHub Desktop.
Linux CLI to kill a pid as nicely but surely as possible
This file contains 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
#!/bin/bash | |
# How to try to kill a process | |
# http://porkmail.org/era/unix/award.html#uuk9letter | |
# Be as nice as possible before shooting it in the head | |
# uses waitpid: https://gist.github.com/tubaman/00b792221bd75fbbca61184da3d414ce | |
options=$(getopt -o v --long verbose -- $@) | |
[ $? -eq 0 ] || { | |
exit 1 | |
} | |
eval set -- "$options" | |
while true; do | |
case "$1" in | |
-v|--verbose) | |
VERBOSE=1 | |
;; | |
--) | |
shift | |
break | |
;; | |
esac | |
shift | |
done | |
[ $# -gt 0 ] || { | |
echo "You must specify at least one pid" | |
exit 1 | |
} | |
test -n "$VERBOSE" && echo 'sending SIGTERM' | |
kill -15 $@ | |
timeout 2 waitpid $@ && exit 0 | |
test -n "$VERBOSE" && echo 'sending SIGINT' | |
kill -2 $@ | |
timeout 2 waitpid $@ && exit 0 | |
test -n "$VERBOSE" && echo 'sending SIGHUP' | |
kill -1 $@ | |
timeout 2 waitpid $@ && exit 0 | |
test -n "$VERBOSE" && echo 'sending SIGKILL' | |
kill -9 $@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment