简体   繁体   English

如何杀死 Python 程序的所有进程?

[英]How to kill all processes of a Python program?

I am running a Python program service_host.py, which started multiple processes.我正在运行一个 Python 程序 service_host.py,它启动了多个进程。 And when I use ps -ef | grep python当我使用ps -ef | grep python ps -ef | grep python

to check the pids, it shows a lot:检查pid,它显示了很多:

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.我在Linux上,不想通过进程ID一一杀死。 Is there a way to kill them by the Python name 'service_host.py'?有没有办法通过 Python 名称“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?是否有可以轻松完成这项工作的终端命令?

Linux has command pkill to kill by name Linux 有命令pkill按名称杀死

pkill python

exact matching精确匹配

pkill -e 'python service_host.py'

check in full name签入全名

pkill -f 'service_host.py'

More in man pkill更多man pkill

BTW: There is also pgrep to get PIDs using names.顺便说一句:还有pgrep可以使用名称获取 PID。

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:您已将其标记为“python”,并且您的脚本接近于做正确的事情,所以这是您的脚本的固定版本——使用cmdline方法将命令行提取为列表:

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.尽管 Linux 命令行的各种可能性当然是可能的。

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

or whatever...管他呢...

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 .使用htop代替,您将能够使用F4过滤进程service_host ,并使用F5查看整个树。 From there you can kill them all at once with F9 ...从那里你可以用F9一次kill他们...

Follow these simple 3 steps:请遵循以下简单的 3 个步骤:

  1. enter htop进入htop
    • optionally press F4 and type service_host to filter the process to kill可选择F4并键入service_host以过滤要杀死的进程
    • optionally press F5 to sort processes as a tree.可选择F5将进程排序为树。 This organizes child processes to its primary process ID (PID)这会将子进程组织到其主进程 ID (PID)
  2. scroll the list and highlight the process to kill , then press F9 for kill options滚动列表并突出显示要杀死的进程,然后按F9选择终止选项
  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突出显示15 SIGTERM (信号终止)以优雅地请求停止进程或9 SIGKILL (信号终止)以强制关闭进程并最后按 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!我通常 go 和9 SIGKILL不用担心太多,但这取决于你和你在 python 脚本中所做的事情!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM