在我的脚本中,我得到了一个进程id(parent/main)
对于id为3-4的主进程,有几个子进程在linux终端上运行。
我的需求来自那个process _ and,它也会杀死所有子进程和子进程。
我试过了
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.then in循环列出所有子进程,如何杀死这些子进程,然后是父进程。
kill process and its sub/co-processes by getting their parent pid by python script
遗憾的是,这对我的情况没有帮助。
发布于 2015-12-17 18:32:11
使用psutil可以做到这一点。
import signal
import psutil
def kill(ppid, signal=signal.SIGTERM):
try:
process = psutil.Process(ppid)
except psutil.NoSuchProcess:
return
pids = process.get_children(recursive=True)
for pid in pids:
os.kill(pid.pid, signal)https://stackoverflow.com/questions/34331835
复制相似问题