How to kill all processes of a Python program?
Asked Answered
S

3

6

I am running a Python program service_host.py, which started multiple processes. And when I use ps -ef | grep python

to check the pids, it shows a lot:

congmin  26968 22897  0 Jun20 ?        00:00:00 python service_host.py
congmin  26969 22897  0 Jun20 ?        00:00:00 python service_host.py
congmin  26970 22897  0 Jun20 ?        00:00:00 python service_host.py
congmin  26971 22897  0 Jun20 ?        00:00:00 python service_host.py
congmin  26972 22897  0 Jun20 ?        00:00:00 python service_host.py
congmin  26973 22897  0 Jun20 ?        00:00:00 python service_host.py
congmin  26974 22897  0 Jun20 ?        00:00:00 python service_host.py
congmin  26975 22897  0 Jun20 ?        00:00:00 python service_host.py

What's the best way to kill all these processes at once? I am on Linux and don't want to kill one by one through the process ID. Is there a way to kill them by the Python name 'service_host.py'? I tried this but it didn't kill at all:

import psutil

PROCNAME = "service_host.py"

for proc in psutil.process_iter():
    # check whether the process name matches
    if proc.name() == PROCNAME:
        proc.kill()

Is there a terminal command that can do this job easily?

Spectacular answered 29/6, 2020 at 23:27 Comment(5)
in shell pkill python ?Sonia
I dont' want to kill all python processes. only those associated with the name 'service_host.py'Spectacular
then try pkill service_host.py or maybe pkill -e 'python service_host.py'Sonia
No, it doesn't kill at all.Spectacular
pkill -f 'service_host.py'. This does the job. Thanks.Spectacular
M
3

Using htop instead you’ll be able to filter with F4 the processes service_host instead and have a look at the whole tree with F5. From there you can kill them all at once with F9 ...

Follow these simple 3 steps:

  1. enter htop
    • optionally press F4 and type service_host to filter the process to kill
    • optionally press F5 to sort processes as a tree. This organizes child processes to its primary process ID (PID)
  2. scroll the list and highlight the process to kill, then press F9 for kill options
  3. highlight 15 SIGTERM (signal terminate) to gracefully request to stop the proccess(es) or 9 SIGKILL (signal kill) to forcefully shut down the process(es) and finally press enter

I usually go with 9 SIGKILL without worrying too much, but it's up to you and what you're doing within your python script!

Manwell answered 29/6, 2020 at 23:33 Comment(3)
Exactly how to do this? htop first, seond, press f4, then press f5?Spectacular
Yes, htop is like the more familiar top command, but is not always there, anyway this is for sure a great opportunity to get it sudo apt-get install htop then you'll find it very useful in such situation. Anyway, top should work as well, look for the process and then press the k key to kill that PID, that should be unique for all the child processes ;)Manwell
Anyway, exactly you have to follow these steps I just summarised here above, editing my answer.Manwell
S
16

Linux has command pkill to kill by name

pkill python

exact matching

pkill -e 'python service_host.py'

check in full name

pkill -f 'service_host.py'

More in man pkill

BTW: There is also pgrep to get PIDs using names.

Sonia answered 29/6, 2020 at 23:53 Comment(0)
N
5

You've tagged this with "python" and your script was close to doing the right thing, so here is a fixed version of your script -- use the cmdline method to extract the command line as a list:

import psutil

PROCNAME = "service_host.py"

for proc in psutil.process_iter():
    # check whether the process name matches
    cmdline = proc.cmdline()
    if len(cmdline) >= 2 and "python" in cmdline[0] and cmdline[1] == PROCNAME:
        # print(proc)
        proc.kill()

although various possibilities from the Linux command line are of course possible.

kill `ps -ef | grep "python service_host.py" | grep -v grep | awk '{print $2}'`

or whatever...

Nifty answered 29/6, 2020 at 23:46 Comment(0)
M
3

Using htop instead you’ll be able to filter with F4 the processes service_host instead and have a look at the whole tree with F5. From there you can kill them all at once with F9 ...

Follow these simple 3 steps:

  1. enter htop
    • optionally press F4 and type service_host to filter the process to kill
    • optionally press F5 to sort processes as a tree. This organizes child processes to its primary process ID (PID)
  2. scroll the list and highlight the process to kill, then press F9 for kill options
  3. highlight 15 SIGTERM (signal terminate) to gracefully request to stop the proccess(es) or 9 SIGKILL (signal kill) to forcefully shut down the process(es) and finally press enter

I usually go with 9 SIGKILL without worrying too much, but it's up to you and what you're doing within your python script!

Manwell answered 29/6, 2020 at 23:33 Comment(3)
Exactly how to do this? htop first, seond, press f4, then press f5?Spectacular
Yes, htop is like the more familiar top command, but is not always there, anyway this is for sure a great opportunity to get it sudo apt-get install htop then you'll find it very useful in such situation. Anyway, top should work as well, look for the process and then press the k key to kill that PID, that should be unique for all the child processes ;)Manwell
Anyway, exactly you have to follow these steps I just summarised here above, editing my answer.Manwell

© 2022 - 2024 — McMap. All rights reserved.