Kill the undesired UNIX processes in one go

Contributor Icon Contributed by roshi Date Icon October 14, 2004  
Tag Icon Tagged: UNIX

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


The following is the general syntax of this 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 : One should give the processname_pattern carefully as an incorrect
regular expression may lead to even desired processes being killed.

Previous recipe | Next recipe |
 
  • Anonymous
    Sorry to appear thick, but could you give an example of processname-patern
    thanks for posting this great help jproctor
  • qmchenry
    That's a good question. Using grep has a brief but steep learning curve. Most UNIX commands do. Once you get the basics, you can put grep to powerful use.

    In the most basic usage, grep will search through text looking for the search string. If you want to look for commands containing the string 'http' you would simply use:

    ..... | grep http | ....


    Grep stands for global regular expression parser and is a magnificently powerful tool. The important thing to take out of that mouthful is 'regular expression' which is a formal language for representing elaborate wildcards for searching. For example, a period matches any single character, so .at would match bat, cat, rat, and so on. Used in a grep statement, it would match that regular expression anywhere in the line, so it would also match brat and bats. Regular expressions would be a great idea for a series of rec.pes.
  • casper
    i think you might want to look into the killall command
  • Holger
    a kill -9 doesnt give the process the possibility to clean up; to exit "voluntarily". can screw up its data.

    always try a kill (without -9) first
  • Anonymous
    You can use the following command :

    kill -9 `ps -ef | grep <process-name> | grep -v grep | awk '{print $2}'`

    Just make sure that you keyin the correct process name in the above command.
  • Anonymous
    <ul id="quote"><h6>jproctor wrote:</h6>Sorry to appear thick, but could you give an example of processname-patern
    thanks for posting this great help jproctor</ul>

    you can use pgrep and pkill
  • Anonymous
    Hi,
    PID TTY COMMAND
    2345 ? 0:00 msgcntl //
    4977 pts/tc 0:00 ps
    3807 ? 0:00 spectrum
    3813 ? 0:00 msgcntl //
    3802 ? 0:00 printmgr
    3814 ? 0:00 ssmcntl//
    3810 ? 0:00 ptrcntl//
    3815 ? 0:00 ssmcntl//
    3811 ? 0:00 termcntl//
    3817 ? 0:00 ssmcntl//
    3816 ? 0:00 pccntl//
    3798 ? 0:00 rsetcntl//
    2215 pts/tc 0:00 sh

    <font color="blue">In the above scenario, I have to kill all the process except ps and sh . How to do that ?</font>
  • Anonymous

    egrep -v "ps|sh|PID" file | awk '{print $1}' | xargs kill
blog comments powered by Disqus