Kill a linux process by name

The kill command is used to kill a process using its pid, but there are times when you need to kill the process by its name.

killall “command” is usually used to do this, but sometimes there are issues where it returns “No matching processes belonging to you were found”. Killall isn’t the greatest at finding the correct process name (especially when your process is being used by an interpreter).

NOTE: Keep in mind that “ps ax” and “ps aux” return two different command names, a long form and short form respectively.

I ran into this issue, so I wrote the following to find the process I needed to kill much easier.

kill -15 `ps ax | grep “[0-9] <command>” | awk -F” ” ‘{print $1}’`

NOTE: You must replace <command> with the command you like.

This gets the specific pid for your chosen command and passes it to the kill application.

Specifically, I needed to kill a nodejs script that was being interpreted by the node interpreter.

This had to be done in a crontab every X minutes. Killall wasn’t finding the process on Ubuntu, but it seemed to be working on OSX so I wrote this method that worked on both OSs.

kill -15 `ps ax | grep “[0-9] node bin/server.js” | awk -F” ” ‘{print $1}’`

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.