Re: kill all the process of a particular user (443 Views)
Reply
Occasional Advisor
Yashodhan Deo
Posts: 18
Registered: ‎06-22-2004
Message 1 of 12 (443 Views)

kill all the process of a particular user

I am new to unix as well as scripting. Can u guys tell me how to kill all the process for a particular user by using a script. Also is there any way I can keep a track of what all commands are executed by the users logging in to the system.

Thanks in advance
Honored Contributor
Michael Tully
Posts: 7,905
Registered: ‎04-15-1999
Message 2 of 12 (443 Views)

Re: kill all the process of a particular user

As long as user have a korn shell they can be capured in their own .sh_history file, that is of course if they are used. The only real way is turn on system auditing, but this does use system resources.

As far as a script is concerned, there are plenty shoudl you do a search. Here is one sample that has some.

http://forums1.itrc.hp.com/service/forums/questionanswer.do?admit=716493758+1097710814724+28353475&t...
Anyone for a Mutiny ?
Honored Contributor
Sundar_7
Posts: 1,938
Registered: ‎11-07-2000
Message 3 of 12 (443 Views)

Re: kill all the process of a particular user

To kill all the processes of a user

# vi /usr/local/bin/kill-all.sh
#!/usr/bin/sh
if [ $# -lt 1 ]
then
echo "Usage: kill-all.sh "
exit 1
fi
USER=$1
ps -fu $USER 1>/dev/null 2>&1

if [ $? -ne 0 ]
then
echo "User $USER is unknown or do not own any process in the system"
exit 0
else
ps -fu $USER | grep -v UID | awk '{print $2}' | xargs -n1 kill
sleep 5
ps -fu $USER | grep -v UID | awk '{print $2}' | xargs -n1 kill -9
if [ $(ps -fu $USER | grep -v UID | wc -l) -gt 0 ]
then
echo "Could not successfully kill all the user's processes"
exit 1
else
echo "Successfully killed all the user's processes"
exit 0
fi
fi
#
Learn What to do ,How to do and more importantly When to do ?
Occasional Visitor
Vali Ticau
Posts: 4
Registered: ‎10-14-2004
Message 4 of 12 (443 Views)

Re: kill all the process of a particular user

Couldn't resist to post yet another (perl) solution !

Honored Contributor
Muthukumar_5
Posts: 4,030
Registered: ‎06-09-2004
Message 5 of 12 (443 Views)

Re: kill all the process of a particular user

We can do with ps -u to collect all process related to user there.

We can do with a one line script as,

kill -9 `ps -u | grep -v 'PID TTY' | awk '{ print $1 }'


If you want to kill process related to user and more based on terminal type then,

we can collect terminal information + user with who -Ru command there. We can also kill user based on their remote login IP there.


We can get the specific users commands execution with their history file there. You can collect that history file as,

FILE=$(su -c "echo $HISTFILE")
echo "Command usage by "
cat $FILE

We have to have root permission to do this with out problem / passwd prompting there.

By combining both,
#!/bin/sh
echo "Enter username to be killed and monitored"
read user

kill -9 `ps -u $user | grep -v 'PID TTY' | awk '{ pritn $1 }'`
cat `su $user -c "echo $HISTFILE"`
# end #

HTH.
Easy to suggest when don't know about the problem!
Honored Contributor
T G Manikandan
Posts: 5,533
Registered: ‎12-11-2000
Message 6 of 12 (443 Views)

Re: kill all the process of a particular user

to kill all processes of a particular user

#ps -ef|grep |grep -v grep|awk -F " " '{print "kill -9" $2}'|sh

Respected Contributor
Franky_1
Posts: 322
Registered: ‎01-14-2001
Message 7 of 12 (443 Views)

Re: kill all the process of a particular user

Hi,

another soultion would be

for i in `ps -fu |grep -v PID|awk '{print $2}'`
do
(echo $i)
kill (-9) $i
done

Regards

Franky
Don't worry be happy
Advisor
Sandeep Kapare
Posts: 16
Registered: ‎05-26-2003
Message 8 of 12 (443 Views)

Re: kill all the process of a particular user

Oneliner for the same is -
kill -9 `ps -aef | grep USERNAME | grep -v grep | awk '{ print $2 }'`

replace USERNAME with the name of the user who you want to kill.
Nothing is impossible
Honored Contributor
Dietmar Konermann
Posts: 1,207
Registered: ‎02-04-1998
Message 9 of 12 (443 Views)

Re: kill all the process of a particular user

I like lean solutions...
# kill $(UNIX95= ps -u $USER -o pid=)
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
Esteemed Contributor
Armin Kunaschik
Posts: 353
Registered: ‎08-30-1999
Message 10 of 12 (443 Views)

Re: kill all the process of a particular user

su - _user_
kill -9 -1

Don't try this with root or other UID0-users :-)
And now for something completely different...
Frequent Advisor
MAUCCI_2
Posts: 48
Registered: ‎06-25-2001
Message 11 of 12 (443 Views)

Re: kill all the process of a particular user

Armin's solution is the one I have been using for years and is the best looking to me ;-)

++Cyrille
Honored Contributor
Bill Hassell
Posts: 14,205
Registered: ‎05-29-2000
Message 12 of 12 (443 Views)

Re: kill all the process of a particular user

Be very, very careful with kill -9. It should never be used to terminate any processes unless absolutely necessary. kill -9 leaves temporary files, orphaned shared memory segments, semaphores out of synce, databases corrupted, etc. The best way to kill a user's session is to determine the parent process and use kill -15 (also known as kill -SIGTERM or kill -TERM).

To determine all the user's sessions, you can use the -H option to ps as in:

UNIX95= ps -fHu user_name

Then you only need to kill the top level process and the rest of the processes should terminate normally.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.