12

I need to kill a few Python processes. I can get a list of the process numbers using pgrep python, but how can I kill them all at once instead of killing one by one?

I'm looking for something like:

pgrep python | kill process_nos
2
  • 2
    man killall...
    – Alex P.
    Commented Apr 16, 2014 at 23:57
  • Besides possibly being a snide remark, "man killall" is not a correct answer. How would killall work with a Python script launched using "python /foo.py"? How about if that python cron were launched from a cron which first invokes a wrapper script? Commented Mar 2, 2016 at 16:33

5 Answers 5

14

You can try:

pgrep python | xargs kill
3
  • 1
    Why would you invoke THREE processes when one suffices?
    – tink
    Commented Apr 17, 2014 at 9:23
  • 1
    For one process it would be kill $(pgrep name) Commented Feb 8, 2015 at 17:56
  • 1
    No, "kill $(pgrep name)" is still two processes and a shell. For one process, use pkill (as pointed out by @tink: superuser.com/a/742741)
    – vog
    Commented Apr 24, 2018 at 7:41
27
pkill python

Short and sweet, man pkill for details.

3
  • I found that pkill python does not always work, in cases where kill PID neither works. But pgrep python | xargs kill -9 does. Can pkill do the equivalent?
    – Jeppe
    Commented Nov 17, 2019 at 12:29
  • 4
    Absolutely ... pkill -9 <...>
    – tink
    Commented Nov 17, 2019 at 16:44
  • 1
    Bare in mind that it the process is stuck on e.g. broken I/O that, too, may not work. But in those cases only a reboot will ...
    – tink
    Commented Nov 17, 2019 at 16:45
1

@tink has the correct answer, but I wanted to add that you want to make sure you are using the correct format for your machine. If you are using a Linux machine, pkill python is correct, but if you are using a Mac terminal, you will want to use pkill Python. So the most correct answer would be this:

pkill <process_name>

where <process_name> is the case-sensitive name of the process to kill.

Note: I understand that the tags for this question were related to Ubuntu Linux, but I wanted to clarify this for anyone that has a different machine but had this exact question (like myself).

1

If pkill is a bit too radical for your tastes and you prefer to select which entries of the pgrep listing you want to kill, you can have a look at ezkill <https://github.com/Kraymer/ezkill> that i wrote.

0

Simply just run -

sudo kill -9 `pgrep python`

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .