Kill the Undesired UNIX Processes in One Go

Posted October 14, 2004 by roshi in UNIX

This tech-recipe is useful when you want to kill all UNIX processes belonging to one user or having a particular regular expression in the process name. You can use different options of “ps” and “grep” to kill the undesired process(es).

The following is the general syntax of this tech-recipe

ps -u | grep | awk '{print $1}' | xargs kill -9

ps -u will find all the processes of the user username.
This output is then greped for processname_pattern which is then piped to awk.
awk ‘{print $1}’ will print only the first column of the output (the process-id, in this case).
This is then xargd to the kill with sure kill -9 signal.

As a result, all the undesired processes will be killed.

Note : You should give the processname_pattern carefully because an incorrect
regular expression may lead to the killing of desired processes.

The Conversation

Follow the reactions below and share your own thoughts.