-
-
Save ahgittin/3734679 to your computer and use it in GitHub Desktop.
script to quickly list find and kill processes
This file contains hidden or 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 | |
help() { | |
echo "pss [command [option]]+ -- quickly list, find and kill processes" | |
echo "commands:" | |
echo " h -- help" | |
echo " g X -- grep X" | |
echo " gi X -- grep -i X (ignore case)" | |
echo " gv X -- grep -v X (show non-matching)" | |
echo " g2 X Y -- grep for X or Y (also g3, g4)" | |
echo " k -- kill" | |
echo " k9 -- kill -9" | |
echo " pid -- extract PID (2nd col)" | |
echo " j -- grep [j]ava" | |
echo " jg X -- j g X" | |
echo " jj -- j then filter useful args" | |
echo "" | |
echo "examples (showing some which can be combined):" | |
echo " pss jj gi test -- summary info of tests running" | |
echo " pss jg jboss k -- kill all jboss java instances" | |
echo " pss g3 java mysql nginx -- see what 3-tier software we have running" | |
echo " pss g3 java mysql nginx k9 - force kill all instances of these guys" | |
} | |
doGrep() { | |
X2=(`grep "$@" <<<"${X[*]}"`) | |
X=(${X2[@]}) | |
} | |
PID_COL=2 | |
pid() { | |
if [[ -z "$PID" ]]; then | |
PID=true | |
X2=(`awk '{print $'$PID_COL'}' <<<"${X[*]}"`) | |
IFS=" " | |
X=(${X2[*]}) | |
fi | |
} | |
filterJavaArgs() { | |
if [[ -z "$FILTER" && -z "$PID" ]]; then | |
PID_COL=1 | |
X2=(`awk '{ x=$2" "$11; for (i=12; i<=NF; i++) { | |
skip=0; | |
if (substr($i,1,2)=="-D") skip=1; | |
else if (substr($i,1,2)=="-X") skip=1; | |
else if ($i=="-jar") skip=1; | |
else if (substr($i,1,10)=="-classpath") skip=2; | |
else if (substr($i,1,3)=="-cp") skip=2; | |
else if (substr($i,1,2)=="--") skip=2; | |
if (skip==0) x=x" "$i | |
else i+=(skip-1) | |
}; print x }' <<<"${X[*]}"`) | |
X=(${X2[*]}) | |
fi | |
} | |
CRLF=" | |
" | |
run() { | |
IFS=$'\n' | |
PS=(`ps auxwww`) | |
X=(${PS[@]}) | |
while [[ ! -z "$1" ]] ; do | |
if [[ "$1" == "g" ]] ; then doGrep "$2" ; shift 2 ; | |
elif [[ "$1" == "g2" ]] ; then doGrep -i "$2$CRLF$3" ; shift 3 ; | |
elif [[ "$1" == "g3" ]] ; then doGrep -i "$2$CRLF$3$CRLF$4" ; shift 4 ; | |
elif [[ "$1" == "g4" ]] ; then doGrep -i "$2$CRLF$3$CRLF$4$CRLF$5" ; shift 5 ; | |
elif [[ "$1" == "gi" ]] ; then doGrep -i $2 ; shift 2 ; | |
elif [[ "$1" == "gv" ]] ; then doGrep -v $2 ; shift 2 ; | |
elif [[ "$1" == "j" ]] ; then doGrep "[j]ava" ; shift 1 ; | |
elif [[ "$1" == "jg" ]] ; then doGrep "[j]ava" ; shift 1 ; doGrep $2 ; shift 2 ; | |
elif [[ "$1" == "jj" ]] ; then doGrep "[j]ava" ; filterJavaArgs ; shift 1 ; | |
elif [[ "$1" == "pid" ]] ; then pid ; shift 1 ; | |
elif [[ "$1" == "k" ]] ; then pid ; [[ -z "$X" ]] || kill ${X[*]} ; shift 1 ; | |
elif [[ "$1" == "k9" ]] ; then pid ; [[ -z "$X" ]] || kill -9 ${X[*]} ; shift 1 ; | |
else | |
ERROR="pss -- illegal option $1" | |
echo $ERROR | |
echo "use pss h for help" | |
shift $# | |
fi | |
done | |
if [[ -z "$ERROR" ]]; then | |
echo "${X[*]}" | |
fi | |
} | |
if [[ -z "$1" || "$1" == "h" || "$1" == "-h" || "$1" == "--help" ]] ; then | |
help | |
else | |
run $@ | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment