Ramana Reddy
Ramana Reddy

Reputation: 399

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

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?

Upvotes: 0

Views: 1596

Answers (1)

msw
msw

Reputation: 43487

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). …

Upvotes: 2

Related Questions