Tidbit: Killing a Linux Process

Posted by ryan
at 5:32 PM on Thursday, May 11, 2006

From the “I always need this but never remember how to do it” file – How to kill a Linux process from the command line:

ps -A | grep processname | cut -c0-5 | xargs kill

This gets a listing of the process in question from ps -A that looks like this:

17468 ? 00:00:00 processname

and cuts the first five characters to get the process id and passes that straight to kill.

For extra crispiness, use kill -9.

Update: As my fellow readers have pointed out – the killall command already does this. So, don’t write your own and don’t listen to me, just use killall processname

However, if you’re looking to kill only certain processes, and not all processes of the same executable, you can modify the command like this for a more generic matcher patter:

ps -aux | grep process pattern | cut -c10-15 | xargs kill

tags:

Comments

Leave a response

  1. Ryan DaigleMay 11, 2006 @ 06:37 PM
    Actually, this script will kill all the named processes - not just individual ones. If you just run this part of the command: bq. ps -A | grep _processname_ | cut -c0-5 you'll see that all the process ids are listed: 1231 128 43231 ... ALL of those pids are then killed. Let me know if it's not working like this for you.
  2. anjan bacchuMay 11, 2006 @ 06:37 PM
    hi there what happens if there are multiple matches for the process name ? At a given time, it will kill only 1 process. How do you kill all the processes of a given name ? The utility pskill(only on windows from sysinternals) will kill (all instances) of a named process or a pid. BR, ~A
  3. MattMay 11, 2006 @ 06:37 PM
    man killall
  4. Ryan DaigleMay 11, 2006 @ 10:43 PM
    Yes, yes... so my post should have been called "how to write your own _killall_". Amazing what you learn from your own blog posts...
  5. JasonMay 11, 2006 @ 10:43 PM
    most (all?) linux distributions provide a killall command that does this for you
  6. nbcMay 24, 2006 @ 10:16 AM
    Another method : ps -A | awk '/processname/{print $1}'