[英]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 个步骤:
htop
htop
F4
and type service_host
to filter the process to killF4
并键入service_host
以过滤要杀死的进程F5
to sort processes as a tree.F5
将进程排序为树。 This organizes child processes to its primary process ID (PID)F9
for kill optionsF9
选择终止选项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 enter15 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.