How to kill all processes with the same name using OS X Terminal

MacosShellKill Process

Macos Problem Overview


Getting the following output from running this:

ps aux | grep Python

Output:

user_name  84487   0.0  0.0        0      0   ??  Z    12:15PM   0:00.00 (Python)
user_name  84535   0.0  0.0        0      0   ??  Z    12:16PM   0:00.00 (Python)

I want to terminate all Python processes currently running on a machine....

Macos Solutions


Solution 1 - Macos

use pkill, with the -f option.

pkill -f python

If you don't have pkill pre-installed (some osx's don't...), try proctools.

Solution 2 - Macos

If you don't have pkill, you can try this:

ps aux | grep python | grep -v grep | awk '{print $2}'

If that gives you the PIDs you want to kill, join that up with the kill command like this

kill $(ps aux | grep python | grep -v grep | awk '{print $2}')

That says... kill all the PIDs that result from the command in parentheses.

Solution 3 - Macos

killall python

Will do the trick.

Solution 4 - Macos

@shx2: Thanks for the trick! Here are the steps to make it work:

Step1:

cd /usr/bin

Step2:

touch "pkill"

Step3: With textEditor of your choice open the file you just created: /usr/bin/pkill (do it with sudo or be Admin). Copy/paste this and save:

for X in `ps acx | grep -i $1 | awk {'print $1'}`; do
  kill $X;
done

Step3: Set file attribute

sudo chmod 755 /usr/bin/pkill

Now you ready to terminate any process using a simple syntax:

For example, to terminate all Python processes open a shell and type:

pkill Python

All python processes should be gone by now.

Solution 5 - Macos

pgrep python | xargs sudo kill -9

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionalphanumericView Question on Stackoverflow
Solution 1 - Macosshx2View Answer on Stackoverflow
Solution 2 - MacosMark SetchellView Answer on Stackoverflow
Solution 3 - MacosBillView Answer on Stackoverflow
Solution 4 - MacosalphanumericView Answer on Stackoverflow
Solution 5 - MacosPablo View Answer on Stackoverflow