OSX Terminal: how to kill all processes with the same name

Getting the following output from 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 the machine.

+16


source to share


5 answers


use pkill with option -f

.

pkill -f python

      



If you don't have it preinstalled pkill

(some osx don't ...) try proctools .

+31


source


If you haven't pkill

, you can try this:

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

      

If this gives you the PIDs you want to kill then attach them to a kill command like this



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

      

That says ... kill all PIDs that are the result of the parenthesized command.

+14


source


killall python

      

Will do the trick.

+5


source


@ shx2: Thanks for the trick! Here are the steps to get it working:

Step 1:

cd /usr/bin

      

Step 2:

touch "pkill"

      

Step 3. Using your chosen text editor, open the file you just created: / usr / bin / pkill (do this with sudo or your administrator). Copy / paste this and save:

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

      

Step 3. Set the file attribute

sudo chmod 755 /usr/bin/pkill

      

You are now ready to terminate any process with a simple syntax:

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

pkill Python

      

All python processes should be removed.

+1


source


@glenn: Your command doesn't send pid out of itself to kill, which will result in "No such process" error. Or am I missing something. Thank.

ps aux | awk '/ firefox / {print $ 2}' | xargs kill kill 1541: No such process

0


source







All Articles