1836771 Members
2146 Online
110109 Solutions
New Discussion

Kill all?

 
SOLVED
Go to solution
Ted Flanders
Frequent Advisor

Kill all?

Is it possible to write a script that would kill a specific group of users with one command? I would like to be able to kill -9 a branch at a time. Maybe I am just dreaming? I run a HP9000 K220 box with HPUX 10.20 on it. I guess the script would have to be able to tell who was on at the time of the process. It gets old having to kill a group of users one at a time (25 or so) when a branch goes down. Thanks!
16 REPLIES 16
Marcel Boon
Trusted Contributor

Re: Kill all?

Hi Ted,

It is possible, you can search at processes with ps -ef |grep user.
After that you can search in the /etc/group file for the special user.

Or you start the other way. first grep the group in the /etc/group, read those users of the group and grep in the proces list with this users.

marcel
See the man pages
Alexander M. Ermes
Honored Contributor

Re: Kill all?

Hi there.
Perhaps you can try this little script :

ps -ef|grep ${1}|grep -v grep | sort | awk | '{print "kill -9 "$2}'> /usr/tmp/kill_em

chmod 744 /usr/tmp/kill_em

/usr/tmp/kill_em

ps -ef|grep ${1}

Only usable as superuser.
Rgds
Alexander M. Ermes
.. and all these memories are going to vanish like tears in the rain! final words from Rutger Hauer in "Blade Runner"
CHRIS_ANORUO
Honored Contributor

Re: Kill all?

Try ee=`ps -e|grep processname|cut -c2-6`
kill -9 $ee
When We Seek To Discover The Best In Others, We Somehow Bring Out The Best In Ourselves.
Charles Darnell
Occasional Advisor

Re: Kill all?

My fav is this,

ps -ef |grep username |awk '{print $2}' |xargs kill -9

=]
James R. Ferguson
Acclaimed Contributor

Re: Kill all?

Hi Ted:

I would suggest that you do a "gentle" kill first, followed by a "kill -9" after waiting a few seconds. This gives the processes being killed the ability to trap the termination signal and do any cleanup processing.

If you build the list of PIDs to kill into a variable as, for instance, suggested by Chris, then you can do something like:

kill $PIDLIST
sleep 5
kill -9 $PIDLIST 2> /dev/null

Redirecting the stderr of the second kill to /dev/null suppresses any (or all) "process does not exist" messages for those already dead.

Regards!

...JRF...
Ted Flanders
Frequent Advisor

Re: Kill all?

So.....forgive me here but.....I want to be able to type in, for example: kill_seattle
and have it kill all users from the seattle branch. So, I would name the script kill_seattle and list usernames to grep? Can you tell that I am inexperienced at this!?!
James R. Ferguson
Acclaimed Contributor

Re: Kill all?

Hi Ted:

Assuming you want to predefine the users in your branch, you could do something simple like this:

#!/usr/bin/sh
SEATTLE="-e usr1 -e usr2 -e usr3"
PIDLIST=`ps -ef|grep $WHO|grep -v grep|awk '{print $2}'`
kill $PIDLIST
sleep 5
kill -9 $PIDLIST
#.end.

The '-e' option allows multiple arguments; and the '-v' option filters out finding the 'grep' process you're running. Watch out for matches to users you don't want. For example, "usr" would match anything in the example script. You could always use regular expressions like:

[[:space:]]usr1[[:space:]]

in lieu of simply "usr1" if/as needed.

...JRF...
Volker Borowski
Honored Contributor

Re: Kill all?

Hmmm,

What about:

kill `ps -u id1 id2 id3 | awk '{ print $1}'`

Volker
Volker Borowski
Honored Contributor

Re: Kill all?

... to be added,

"ps -ef | grep user_to_kill" as selection base
might kill a process with the name /home/user_to_kill/job.sh although it is executed by user_never_to_be_killed !

May be some scripts should be adjusted ?
( ok, may be a rare case :-)

Volker
James R. Ferguson
Acclaimed Contributor

Re: Kill all?

Hi (again):

Here's another way to make the filtering of the 'ps' output more rigorous; choose the processes by user name and first issue a simple kill to allow the process to attempt to cleanup:

#!/usr/bin/sh
PIDLIST=``ps -ef|awk '$1~/^usr1$/||$1~/^usr2$/||$1~/^usr3$/ {print $2}'`
kill $PIDLIST
sleep 5
kill -9 $PIDLIST
#.end.

...JRF...
Keith Bunge
Occasional Advisor

Re: Kill all?

If you are looking for a way to kill the processes running by users within a specific group defined in /etc/groups I personally use this (not sure how well this will paste in). Basically it cats the group file and parses it and then runs a for loop to kill all processes that each user is running.

#!/bin/sh

if [ "$2" = "" ]; then
SIG="-TERM"
else
SIG="$2"
fi

if [ "$1" = "" ]; then
echo "Usage: $0 "
exit
fi

WHO=`cat group | grep ^$1: | awk -F: '{ print $4 }' | sed s/,/\ /g`

for i in $WHO; do
kill $SIG `ps aux | grep $i | grep -v grep | awk '{ print $2 }'`
done
Keith Bunge
Occasional Advisor
Solution

Re: Kill all?

Sorry about that I had taken out all the references to NIS in that script, this is how it should read.

#!/bin/sh

if [ "$2" = "" ]; then
SIG="-TERM"
else
SIG="$2"
fi

if [ "$1" = "" ]; then
echo "Usage: $0 "
exit
fi

WHO=`cat /etc/group | grep ^$1: | awk -F: '{ print $4 }' | sed s/,/\ /g`

for i in $WHO; do
kill $SIG `ps -ef | grep $i | grep -v grep | awk '{ print $2 }'`
done
Ted Flanders
Frequent Advisor

Re: Kill all?

Thanks all, for your help. Time to start testing these scripts! I hope I assigned points correctly. With multiple answers from the same person, I gave half the total points to some answers, add them both up for the total points given. I think that is the right way to do it. Otherwise some people would recieve 20 or 30 points for multiple answers. I dont know, let me know if this is ok, I guess you do take the time to answer. Once again, I dont know what I would do without your expertise!
Volker Borowski
Honored Contributor

Re: Kill all?

Hey Ted,

your fine with the points, although if it is a real solution within one answer, it deserves at least 8 points (to show up the bunny).

If there is a second quote just for additional information, you could choose to add N/A (as you should do for this one), to keep your assignment statistics up to date.
But, if the additional information has good quality, it is also okay to assign additional points.
You will find several messages in this forum, which are over-pointed in my opinion. I.e the jokes that show up from time to time. But on the other hand, some of those are really good, so why not (I imagine this HP-McDonalds comparison a few days ago :~)
That was a well deserved 10 for Rick ....

Best Regards
Volker
Les Schuettpelz
Frequent Advisor

Re: Kill all?

Dave Fargo using Les's ITRC login..

Do everything possible to prevent selecting an unintended process for automated kill.

Use 'ps' options (user, group, etc.) to limit the selection first, relying on 'grep' only as absolutely necessary. Using 'grep ' alone may select processes for other UIDs, if the name is embedded in the process information (IE the directory path to the file being executed), or if you have very similar ID names, such as user1, user10, user11.

Also, once a process is identified for kill, store the process information and use it to verify that the PID still refers to what you want before issuing a kill. When killing groups of processes, parent/child relationships can cause several to die from one kill command, freeing the process table entry for the PID, which could then get grabbed by a new unrelated process. So the script should be smart enough to check that what it first selected is still what is active for the PID.

2 or more loops through the PIDs is a good idea, first doing soft kills, then any app-specific kills, finally the -9 option.
Yvonne Chan
Advisor

Re: Kill all?

Hi Keith Bunge,
I had tried your script but it is not able to capture users in the primary as the /etc/group only shows the secondary group with userids. How should I check for the primary group users? As I had 2 group ids of 100 and 1000 and when I did a grep 100 on /etc/passwd it captures all the users in the group 100 and 1000 which is not what I want. Any advise? Thanks.