是否有办法制造操作系统。killpg没有杀死调用它的脚本?

时间:2021-02-22 21:32:49

I have a subprocess which I open, which calls other processes.

我有一个我打开的子进程,它调用其他进程。

I use os.killpg(os.getpgid(subOut.pid), signal.SIGTERM) to kill the entire group, but this kills the python script as well. Even when I call a python script with os.killpg from a second python script, this kills the second script as well. Is there a way to make os.killpg not stop the script?

我使用os.killpg(os.getpgid(subOut.pid)、sign. sigterm)来杀死整个组,但这也杀死了python脚本。甚至当我用操作系统调用python脚本时。第二个python脚本的killpg也会杀死第二个脚本。是否有办法制造操作系统。killpg没有停止脚本?

Another solution would be to individually kill every child 1process. However, even using

另一个解决办法是逐个杀死每个孩子。然而,即使使用

p = psutil.Process(subOut.pid)
child_pid = p.children(recursive=True)
for pid in child_pid:
    os.kill(pid.pid, signal.SIGTERM)

does not correctly give me all the pids of the children.

没有正确地给我所有的孩子们的孩子。

And you know what they say... don't kill the script that calls you...

你知道他们怎么说…不要扼杀召唤你的剧本……

1 个解决方案

#1


0  

Create a process group having all the immediate children of the called process as follows:

创建一个流程组,将调用流程的所有直接子节点如下:

p1 = subprocess.Popen(cmd1)
os.setpgrp(p1.pid, 0) #It will create process group with id same as p1.pid
p2 = subprocess.Popen(cmd2)
os.setpgrp(p2.pid, os.getpgid(p1.pid))

pn = subprocess.Popen(cmdn)
os.setpgrp(pn.pid, os.getpgid(p1.pid))

#Kill all the children and their process tree using following command
os.killpg(os.getpgid(p1.pid), signal.SIGKILL)

It will kill whole process tree except its own process.

它将杀死整个过程树,除了它自己的过程。

#1


0  

Create a process group having all the immediate children of the called process as follows:

创建一个流程组,将调用流程的所有直接子节点如下:

p1 = subprocess.Popen(cmd1)
os.setpgrp(p1.pid, 0) #It will create process group with id same as p1.pid
p2 = subprocess.Popen(cmd2)
os.setpgrp(p2.pid, os.getpgid(p1.pid))

pn = subprocess.Popen(cmdn)
os.setpgrp(pn.pid, os.getpgid(p1.pid))

#Kill all the children and their process tree using following command
os.killpg(os.getpgid(p1.pid), signal.SIGKILL)

It will kill whole process tree except its own process.

它将杀死整个过程树,除了它自己的过程。