Operating System - HP-UX
1753797 Members
7115 Online
108799 Solutions
New Discussion юеВ

Re: how to kill all the process belongs to a user

 
SOLVED
Go to solution
senthil_kumar_1
Super Advisor

how to kill all the process belongs to a user

Hi

I want kill to all the process in single command that belongs to particular user.

Normally i am killing the process using PID. so it takes time to kill all the process. since one use can start N number of process.

so it will be usefull if i am able to kill all the process run by a user.
11 REPLIES 11
Tingli
Esteemed Contributor

Re: how to kill all the process belongs to a user

ps -eaf -u [uidlist]
James R. Ferguson
Acclaimed Contributor

Re: how to kill all the process belongs to a user

Hi:

If you really want to do this, one way is:

# UNIX95= ps -fu uid -o pid=|xargs -i kill {}

Note that 'uid' in the above command can be either a 'uid' or a username.

The UNIX95 environement is set _only_ for the command line. There is no semicolon after the "=" character and before the 'ps' command.

This command creates a list of all the process IDs (pid) associated with a user (uid) and pipes them to the 'kill' command as a list.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: how to kill all the process belongs to a user

Hi (again):

I should add the following admonishment. Do not begin with a 'kill -9'. A kill -9 cannot be trapped by a process and thus the process has no chance to cleanup temporary files or remove shared memory segments. This is quite undesirable.

Instead, perform a series of 'kill' actions.

Capture your pid list in a variable as:

# PIDS=$(UNIX95= ps -u uid -o pid=)

...where 'uid' is the user name or 'uid' value of interest.

Now do:

# kill -1 ${PIDS}
# kill -15 ${PIDS}
# kill -9 ${PIDS}

This leaves a 'kill -9' as the last resort only.

Regards!

...JRF...

UVK
Trusted Contributor

Re: how to kill all the process belongs to a user

use the below

for i in `ps -u | grep -v PID| awk '{print$1}'`
>do
>kill -9 $i
>done

-------------------------------------------
Like it or worked !! Click kudos !!
UVK
Trusted Contributor

Re: how to kill all the process belongs to a user

That just my way ..you can do it as many ways as u want to do it, but one thing you would like to know is " ps -u "
that will list all the process by a user from that o/p extract the process ID and then kill them.

I know you can figure out a better way than me.

Cheers,
uvk
-------------------------------------------
Like it or worked !! Click kudos !!
James R. Ferguson
Acclaimed Contributor

Re: how to kill all the process belongs to a user

Hi (again):

Although it is probably obvious, when you use the 'kill' sequence I suggested, you will probably want to suppress the warning "The specified process does not exist" for processes that have been terminated by the first or second 'kill'. Thus:

PIDS=$(UNIX95= ps -u uid -o pid=)
kill -1 ${PIDS}
kill -15 ${PIDS} 2>/dev/null
kill -9 ${PIDS} 2>/dev/null

You could also test for an empty list first as:

PIDS=$(UNIX95= ps -u uid -o pid=)
if [ ! -z "${PIDS}" ]; then
kill -1 ${PIDS}
kill -15 ${PIDS} 2>/dev/null
kill -9 ${PIDS} 2>/dev/null
fi

...or you could skip the empty list test and also redirect STDERR to /dev/null for every 'kill' so that an empty list doesn't give an error like: "The number of parameters specified is not correct".

Regards!

...JRF...
Suraj K Sankari
Honored Contributor

Re: how to kill all the process belongs to a user

Hi,

Do the below things

ps -aef | grep username

take the pid's of all process

right a for loop to kill those pid's

Suraj
Jeeshan
Honored Contributor
Solution

Re: how to kill all the process belongs to a user

ps -ef|grep user|awk '{ print $2; }'|xargs kill -9
a warrior never quits
yulianto piyut
Valued Contributor

Re: how to kill all the process belongs to a user

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