DEV Community

Connor Bode
Connor Bode

Posted on

The best way to KILL (..a process on Mac or Linux)

What's the best way to kill a process in Linux (or Mac)?

Ok, I lied, it's actually probably not the best way. There are probably better ways to use only ps to find the processes you're looking for. But this method uses two awesome shell tools, grep and awk, which you can use in other ways.

The final command I use is:

kill -9 $(ps aux | grep "[y]our-process-name" | awk '{print $2}')

That's going to send a SIGKILL to every process you grepped for.

How did we get there?

1. Find the processes you want to shut down

ps aux is going to print out all of the processes currently running, including the name of the user that is running it.

We don't necessarily need all that information right now, but it's a good command to remember if you ever need to see what's running on a system.

Next, we'll filter out the processes we want to find using a search query. Often, I'm trying to kill detached Django dev server instances, which run under the command python manage.py runserver. To filter those out, I'll run ps aux | grep manage.py.

Unfortunately, this also locates the grep process itself (since grep will be running still when ps aux is executed). By the time we get to our kill function, the grep process will be finished, so I use a little trick to filter out the grep: ps aux | grep "[m]anage.py". If you want to skip that, no problem.. you'll just see an error message that you weren't able to kill one of the processes.

2. Get the process IDs

awk is a cool tool designed for processing patterns & text. After we run ps aux | grep "[m]anage.py", we have tabular output. The process ID is in the second column of the output, and awk helps us to only print that second column.

How?

You just pipe your output to awk '{print $2}'.

3. Kill it!

Finally, we wrap all of that up into a subquery, and kill the result:

kill -9 $(ps aux | grep "[m]anage.py" | awk '{print $2}')

Keep in mind that this is a process suicide, not graceful shutdown. You can do a lot more with POSIX signals if you're interested.


Hopefully this tip was helpful for you!

Follow me here on dev.to or on Twitter @connorbode for more on Linux, web, and app development!

Top comments (5)

Collapse
 
jmcphe profile image
James McPhee

In modern linuxes, you have access to pkill -signal <pattern>. Taking your example this would be:
pkill -9 manage.py
You have the ability to limit by user with -u username
You can kill the oldest process of pattern with -o and newest with -n.
One VERY nice option is -f to only match the full commandline. This is great for automated scripts in places where people like adding things to common names.
If you just want the list of process ids, then you have pgrep.

If you're stuck using an older shell, you may not have access to the $( ) subshell. You can be bourne compatible with backticks, but nesting becomes an issue.
Another old unix trick is xargs. Some people find this more natural. You pipe the list to xargs command so something like this:
ps aux | grep pattern | grep -v grep | awk '{ print $2 }' | xargs kill -9
The grep -v grep is another way to avoid the grep process instead of relying on shell to interpret the braces away. This has pros and cons, but you may find it useful.

Collapse
 
moopet profile image
Ben Sinclair

I use killall for that :()

Collapse
 
connorbode profile image
Connor Bode

Heh, that probably is a better option. I did mention that this probably is not the best way to do it, but it's still a nice story to tell with some grep & awk tricks.

Collapse
 
biros profile image
Boris Jamot ✊ /

Or, if you use the fish shell, just type kill and press <tab> to list and select the desired process.

Collapse
 
pavelloz profile image
Paweł Kowalski

Bunch of heartless killers, let them grow! They just want to feel warmth of the CPU and fill their void with memory leaks.