I.T. Security and Linux Administration

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:

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 – 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 “$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.

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.

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

Processing your reply...

There was an error processing your information. Please try again later.

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

Processing your reply...

About This Blog

Tools and tips to assist you in your Linux lifestyle. While deviating sometimes to other operating systems, or off-topic discussions, the focus of this blog is to bring a new life to the Linux world, and hopefully a new insight to the happenings in the Linux and open source community as a whole.