2016-01-21 33 views
0

我有我在後臺運行的命令的代碼片段:如何使用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也被甚至被殺害後

如何做到這一點?

+2

'&'沒有做任何事 - 這是在後臺運行進程的bash語法,並且您沒有通過bash運行它。我認爲如果你在產生這個孩子的時候傳遞'preexec_fn = os.setsid',那麼它應該從它的控制tty中分離出來,並且防止它被殺死。或者,如果您可以修改子流程中的代碼,則可以使其忽略SIGHUP。 –

回答

1

您正在問產生的孩子是否同步,以便您可以執行child.expect(…)和異步&。這些不同意。

你可能想:

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

其中interact被定義爲:

這使子進程的交互式用戶(人在鍵盤)的控制。 ...

相關問題