Killing a Linux Process

A very handy tip from Ryan on how to kill the unwanted Linux process.

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

2 Comments so far

  1. Marc on June 19th, 2006

    Yeah, I was going to mention killall but people have beaten me to it.

    Another way to kill processes is from “top”. Press “k” and type in the pid. I also read a week or so ago about an improved “top” called “htop” which I believe may have let you do killing with the arrow keys instead of having to type the pid. Also, I wouldn’t be surprised if there is some kind of process monitor extension for Emacs, though I’ve never seen one.

  2. Andy on June 21st, 2006

    Cheers Marc for the tip. Will have a look at this “htop” thingy, it’d be brilliant if you can just kill a process by using the arrow keys!! 🙂

Leave a Reply