analytic

Wednesday 24 October 2012

Killing Multiple Processes

I was working on one of the issue today, where in the passwordless ssh was not working for one user.  When I checked, I found more than 3000 defunct processes running on the server owned by a specific user and the system was not allowing to start a new process due to the user's nproc limit. The same user was also running 100's of other processes which were not defunct.  The server was running multiple applications so I didn't had the liberty to reboot the server. Hence, the only option I had was to kill  all the processes which were running under the ownership of that specific user.

In this scenario, killing all the processes one by one is a very tedious task.  Also, since there were many defunct processes running on the server, it was very difficult to identify the parent process and kill the parent process of the defunct process.  I decided to write a long command using awk and grep.

Below is the command I used to kill all the processes owned by that user (xyz) in a single command which saved my lot of time.


linux-ydn1:~ # ps -ef | grep -i xyz| awk '{print "kill -9 ", $2}' | sh


Command explanation : 

1.  The first part of command (ps -ef) is providing the list of all the processes running on the server.
2.  The 2nd part, (grep -i xyz) is searching for processes where word xyz exist.  This can be customized based upon the specific requirement.
3.  The 3rd part is searching for the pid of the running process and prefixing the kill command before the pid.
4.  The fourth and the last command is executing each line printed by the 1st 3 parts of the long command using sh command.

Cheers !!
Vaibhav

1 comment:

  1. I tend to be a bit more or a lazy admin (the fewer keystrokes the better)

    pkill -9 -U xyz

    Take a look at the pkill cmd might save you a bit of typing.

    ReplyDelete