DEV Community

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

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.