我想:
我和subprocess.Popen,os.exec,os.spawn,os.system.但没有成功。
另一种解释问题的方法:如果有人杀死了myexe.exe (Arg0)的“进程树”,如何保护myexe.exe (arg0)?
编辑:相同的问题(没有答案) 这里
编辑:下面的命令不能保证子进程的独立性
subprocess.Popen(["myexe.exe",arg[1]],creationflags = DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP,close_fds = True)
发布于 2013-02-10 03:53:35
若要启动可在父进程退出Windows上之后继续运行的子进程,请执行以下操作:
from subprocess import Popen, PIPE
CREATE_NEW_PROCESS_GROUP = 0x00000200
DETACHED_PROCESS = 0x00000008
p = Popen(["myexe.exe", "arg1"], stdin=PIPE, stdout=PIPE, stderr=PIPE,
creationflags=DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP)
print(p.pid)
Windows进程创建标志是这里。
发布于 2013-02-10 03:36:36
几年前,我在windows上做了类似的事情,我的问题是想要扼杀孩子的进程。
我假设您可以使用pid = Popen(["/bin/mycmd", "myarg"]).pid
运行子进程,所以我不知道真正的问题是什么,所以我猜是在您终止主进程的时候。
这和国旗有关。
我无法证明这一点,因为我没有运行Windows。
subprocess.CREATE_NEW_CONSOLE
The new process has a new console, instead of inheriting its parent’s console (the default).
This flag is always set when Popen is created with shell=True.
subprocess.CREATE_NEW_PROCESS_GROUP
A Popen creationflags parameter to specify that a new process group will be created. This flag is necessary for using os.kill() on the subprocess.
This flag is ignored if CREATE_NEW_CONSOLE is specified.
发布于 2015-02-18 11:01:32
所以如果我明白你的意思,代码应该是这样的:
from subprocess import Popen, PIPE
script = "C:\myexe.exe"
param = "-help"
DETACHED_PROCESS = 0x00000008
CREATE_NEW_PROCESS_GROUP = 0x00000200
pid = Popen([script, param], shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE,
creationflags=DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP)
至少我试过这个并且为我工作。
https://stackoverflow.com/questions/14797236
复制相似问题