如何使用pexpect.spawn命令在后台运行?
问题描述:
我有我在后台运行的命令的代码片段:如何使用pexpect.spawn命令在后台运行?
from sys import stdout,exit
import pexpect
try:
child=pexpect.spawn("./gmapPlayerCore.r &")
except:
print "Replaytool Execution Failed!!"
exit(1)
child.timeout=Timeout
child.logfile=stdout
status=child.expect(["[nN]o [sS]uch [fF]ile","",pexpect.TIMEOUT,pexpect.EOF])
if status==0:
print "gmapPlayerCore file is missing"
exit(1)
elif status==1:
print "Starting ReplayTool!!!"
else:
print "Timed out!!"
这里,虽然它在后台运行脚本退出的过程中spawn
也被甚至被杀害后
如何做到这一点?
答
您正在问产生的孩子是否同步,以便您可以执行child.expect(…)
和异步&
。这些不同意。
你可能想:
child=pexpect.spawn("./gmapPlayerCore.r") # no &
status=child.expect(["[nN]o [sS]uch [fF]ile","",pexpect.TIMEOUT,pexpect.EOF])
child.interact()
其中interact被定义为:
这使子进程的交互式用户(人在键盘)的控制。 ...
'&'没有做任何事 - 这是在后台运行进程的bash语法,并且您没有通过bash运行它。我认为如果你在产生这个孩子的时候传递'preexec_fn = os.setsid',那么它应该从它的控制tty中分离出来,并且防止它被杀死。或者,如果您可以修改子流程中的代码,则可以使其忽略SIGHUP。 –