Operating System - HP-UX
1752680 Members
5217 Online
108789 Solutions
New Discussion юеВ

Re: 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 11
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
New Member

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
Sandeep Kapare
Advisor

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...