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
for PID in `cat $filePID`
kill -9 $PID

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.


4 thoughts on “Loop Kill Mutiple PID On Bourne Shell”

  1. kill -9 $(ps -unobody -opid | sed 1d)
    …except I’d *never* script a kill -9 without massive amounts of sanity and error checking.

  2. geeez, you guys are all correct…

    xargs is sooo true… pkill is not for everyone, well at least for solaris 2.6 and below, and kill -9 is indeed not for kids.

Leave a Reply

Your email address will not be published. Required fields are marked *