Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

kill all the process of a particular user

Yashodhan Deo
Occasional Advisor

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
11 REPLIES
Michael Tully
Honored Contributor

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&threadId=250406
Anyone for a Mutiny ?
Sundar_7
Honored Contributor

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 ?
Vali Ticau
Occasional Visitor

Re: kill all the process of a particular user

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

Muthukumar_5
Honored Contributor

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!
T G Manikandan
Honored Contributor

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

Franky_1
Respected Contributor

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

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
Dietmar Konermann
Honored Contributor

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)
Armin Kunaschik
Esteemed Contributor

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...
MAUCCI_2
Frequent Advisor

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
Highlighted
Bill Hassell
Honored Contributor

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.


Bill Hassell, sysadmin