Last active
March 10, 2018 00:26
-
-
Save suewonjp/82183aa403003fa1f1624c3fde3561a0 to your computer and use it in GitHub Desktop.
Bash function that lets you interactively kill arbitrary process with pattern matching (See the comment below for an example usage)
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
killProcess() { | |
if [ $# -lt 1 ]; then | |
echo "Usage: ${FUNCNAME[0]} [pattern for process name]" | |
return | |
fi | |
local procs=() selected= | |
case "$( uname )" in | |
Darwin*|CYGWIN*) mapfile -t procs < <( pgrep -fil "${1}" ) ;; | |
*) mapfile -t procs < <( pgrep -fl "${1}" ) ;; | |
esac | |
selected=$( selectFromArray procs ) | |
if [ "${selected}" ]; then | |
local ans tmp pid | |
read -ra tmp <<< "${selected}" | |
printf "\e[1;36m${tmp[@]}\e[0m\n" | |
pid=${tmp[0]} | |
while :; do | |
read -rn 1 -p "Kill it? (Y/y/n/?) :" ans | |
case "$ans" in | |
Y) echo; kill -9 "${pid}"; return ;; | |
y) echo; kill "${pid}"; return ;; | |
n) echo; return ;; | |
?) printf "\nType \e[4;33m%s\e[0m for normal kill, \e[4;33m%s\e[0m for force kill, \e[4;33m%s\e[0m for abort\n" \ | |
"y" "Y" "n" | |
;; | |
esac | |
done | |
fi | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example