Script to find process by name in args and kill all instances of it

Had issue where weblogic spawned a bunch of PID's on redhat and wouldn't shutdown gracefully so I used script below to find all instances where -Dweblogic.Name=appname to kill all the processes

Save the script below as argskill.sh and make executable then run it as root with your argument - i.e. appname

WARNING - make sure that your argument is unique and that you are not going to kill anything you don't want to. I verify first by running:

ps ww -e -o pid,args |grep <args name> and making sure it doesn't return any extra processes with similar names that I don't want to kill

#!/bin/bash

if [ $1 -eq ""]; then
echo "Usage : ./argskill.sh <args name>"
else
get_proc=`ps ww -e -o pid,args | grep $1`
echo $get_proc >> pids
get_pid=`gawk -F" " '{ print $1 }' pids`
kill -9 $get_pid
fi

Run with:

./argskill.sh appname

No comments: