【发布时间】:2016-03-23 18:12:46
【问题描述】:
在我的脚本中,我得到了一个进程 ID(父/主)
对于 id 3-4 的主进程,linux 终端中正在运行几个子进程
我的要求是从那个 processid 杀死所有子进程和子进程。
我试过了
import os
pid = parent process id
from subprocess import call
call(["pkill", "-TERM","-P", str(pid)])
但没有成功。
也试过了
os.system('kill -9 ' + pid) # only parent is getting killed subpid are still running.
请提出一些建议,例如按主进程 id 列出所有子进程。然后在循环中如何杀死这些子进程,然后是父进程。
kill process and its sub/co-processes by getting their parent pid by python script
遗憾的是,这对我的情况没有帮助。
【问题讨论】:
-
@JohnZwinck-也没有帮助。
-
为什么没有帮助?您对这些解决方案有什么具体问题?
-
@Satya:我认为您需要追求的第一步是“创建一个进程组”,其中包含您以后希望杀死的所有进程。换句话说,这不仅仅是如何在 Python 中解决这个问题,而是通过使用进程组来让问题本身消失,这是在 *nix 系统上解决这个问题的常规方法。
标签: python subprocess kill-process