Skip to content

Instantly share code, notes, and snippets.

@tubaman
Last active June 23, 2020 23:42
Show Gist options
  • Save tubaman/377458a11fedb49cc2132ea6e31ae8b2 to your computer and use it in GitHub Desktop.
Save tubaman/377458a11fedb49cc2132ea6e31ae8b2 to your computer and use it in GitHub Desktop.
Linux CLI to kill a pid as nicely but surely as possible
#!/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