57

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....

jww
  • 97,681
  • 90
  • 411
  • 885
alphanumeric
  • 17,967
  • 64
  • 244
  • 392

6 Answers6

122

use pkill, with the -f option.

pkill -f python

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

shx2
  • 61,779
  • 13
  • 130
  • 153
  • Actually, some of the answers will quietly fail, not killing the processes. Namely, in case-sensitive environments (like Mac, Unix, etc) you should take care of process name -> "Python" instead of "python" and in addition, you might need "sudo". So, here are the options: `sudo killall Python` or `sudo pkill -f Python` – Sinisa Rudan Aug 16 '22 at 14:41
36

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.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • 9
    don't need grep here: `ps aux | awk '/python/ {print $2}' | xargs kill` – glenn jackman Mar 11 '14 at 19:53
  • 1
    Glenn: That is more elegant than my answer, please go ahead and suggest it yourself and grab the points - you won't offend me. I'll vote for you! Also, please tell me how I can refer to your name (which contains a space) using the @ mechanism. Thank you. – Mark Setchell Mar 11 '14 at 20:28
  • 1
    I think if you just use "@glenn", that will notify me. Usually in a comment, you can type "@gl" and my name will be auto-suggested in which case you can hit Tab to complete. However if you're replying to the previous comment, the system figures out who you're replying to. It all pretty much just works. Regarding the points, my code was just a refinement of yours, it's not a substantially new answer. – glenn jackman Mar 11 '14 at 21:19
  • 1
    kill -9 $(ps aux | grep python | grep -v grep | awk '{print $2}') worked for me – jbustamovej Jun 02 '16 at 08:52
  • Brilliant, saved me from restart. Because I had processes spawning all the time in my terminal I got the message that "fork: Resource temporarily unavailable". I tried to kill processes in Activity Monitor but that was too slow. So I quit some programs that were running - Mail, Safari - and very quickly switched to terminal and ran the ps aux.... command - BAM. 1,300 processes killed! – n13 Jan 29 '18 at 01:41
  • Note that the "grep"-based solution provided by Mark is cross-platform. The "awk"-based solution suggested by Glenn does nothing on OS X. The only change I made to this answer is to "grep -i python" so it catches both Python and python. – horace May 19 '23 at 02:55
9
killall python

Will do the trick.

BillRobertson42
  • 12,602
  • 4
  • 40
  • 57
3

@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.

alphanumeric
  • 17,967
  • 64
  • 244
  • 392
1

Actually, some of the answers will quietly fail, not killing the processes. Namely, in case-sensitive environments (like Mac, Unix, etc) you should take care of process name -> "Python" instead of "python" and in addition, you might need "sudo".

So, here are the options: sudo killall Python or sudo pkill -f Python

Sinisa Rudan
  • 561
  • 8
  • 10
0
pgrep python | xargs sudo kill -9
Francesco
  • 897
  • 8
  • 22
Pablo
  • 11
  • 1