I.T. Security and Linux Administration

May 20 2011   10:10AM GMT

Mass process kill using Bash, ps, and awk



Posted by: Eric Hansen
Tags:
awk
bash scripting
kill
process
ps

I’ve been looking for something like this for a while, and have never seen it. I know it exists somewhere, as does most things these days, but I was too lazy to search through the archives of Google, and wrote a script myself. What this does is take each line of the output from ps, and sends a signal (kill -s) to the process ID to kill it.

First, I’ll showcase the (probably can be done in an easier manner) code:

#!/bin/sh

SCRIPT="$0"

if [ -z "$2" ]; then
        sig=9
else
        sig="$2"
fi

ps -eo pid,args | grep "$1" | grep -v grep | grep -v "$SCRIPT" | while read line
do
        pid=`echo -n "$line" | awk '{print $1}'`
        echo "Killing process id $pid with signal $sig..."
        kill -$sig "$pid"
done

The if…fi code block is pretty simple. It checks to see if a signal number (1-15) was passed (though, boundary checking can be done, I just did this quick and dirty), and if not, it auto-assigns the signal as 9 (nicest way to kill a process). Personally, I prefer 15, but as I was testing this, I didn’t want to chance breaking my system.

The following line, however, can lead to some confusion (and is also why I said earlier this can probably be done easier):

ps -eo pid,args | grep "$1" | grep -v grep | grep -v "$SCRIPT" | while read line

ps -eo pid,args – This runs the ps command, listing all the processes that are running (the -e switch), and the -o switch lets you choose what is displayed. Originally I used just ps aux and then let awk do the harder work, but then I figured why should I fetch a lot of un-used information. This also makes the script easier to program the rest of the way.

grep “$1″ – The process you’re looking to mass-kill is the first argument passed to the script, so look through all the results for what we want first.

grep -v grep – Don’t bother fetching the process info for the grep command we run.

grep -v “$SCRIPT” – You could do this as grep -v “$0″, but I decided not to (no benefit that I know of). This also ignores fetching information about the script itself. If we don’t have this line, then the script will freeze/stall trying to kill itself.

while read line – Do a while loop, reading each line of output.

Now, the while loop code block:

        pid=`echo -n "$line" | awk '{print $1}'`
        echo "Killing process id $pid with signal $sig..."
        kill -$sig "$pid"

pid=`echo -n “$line” | awk ‘{print $1}’` – Get the process ID using awk to print out the PID. This is the reason why we used the -o pid,args option for ps.

echo “Killing process id $pid with signal $sig…” – This is more just debugging that I left in to make sure the script is doing something.

kill -$sig “$pid” – The command that makes this all come together. Do you have to enclose the PID in quotes? No, in fact, there’s no logic behind me doing it besides I’m used to enclosing all my variables in quotes. But, since it’s just an integer, you can remove the quotes. Also, I was afraid -$sig wasn’t going to work, but it seems to be the only way to pass the signal to kill.

If you use this script, I’m glad it found a use besides with me. As I said, this can most likely be done in an easier manner, but it works and I might perfect it later on.

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: