Loop Kill Mutiple PID On Bourne Shell

Just want to document it here. A colleague request to kill multiple PID owned by different users. They don’t have any special privilege to kill process other than their own.

Here’s a simple for…loop script that will look for and kill each PID it found.

#!/bin/sh -x
ps -ef | grep nobody | awk ‘{print $2}’ > /tmp/PID
filePID=”/tmp/PID”
for PID in `cat $filePID`
do
kill -9 $PID
done
#

Line 1, tells which shell will execute the script

Line 2, gets all the PID to be killed and store them on a file.

Line 3, assigns a variable for the file

Line 4 to 7 is the for…do loop which basically gets each line on the file, store the value on $PID and then kill whatever value is in it on line 6.

Yucky script. Post a better one.

Thanks.