How do I make a command to run in background using pexpect.spawn?

可紊 提交于 2019-12-11 23:34:07

问题


I have a code snippet in which I run a command in background:

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!!"

Here, after the script exits the process in the spawn also gets killed even though it is run in background

How to achieve this?


回答1:


You are asking the spawned child to be synchronous so that you can execute child.expect(…) and asynchronous &. These don't agree with each other.

You probably want:

child=pexpect.spawn("./gmapPlayerCore.r") # no &
status=child.expect(["[nN]o [sS]uch [fF]ile","",pexpect.TIMEOUT,pexpect.EOF])
child.interact()

where interact is defined as:

This gives control of the child process to the interactive user (the human at the keyboard). …



来源:https://stackoverflow.com/questions/34921807/how-do-i-make-a-command-to-run-in-background-using-pexpect-spawn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!