子进程等待脚本完成

Subprocess wait expect script to finish

我正在调用子进程(期望脚本到 运行 一些命令)并且需要等到它完成才能启动另一个函数,但我认为它只会等到 shell命令已完成,而不是预期脚本。 是否可以等待整个过程完成?

 p111 = subprocess.Popen('gnome-terminal -e "perl /tmp/expect',shell=True)  
    os.waitpid(p111.pid,0)
    smo_final()

使用

https://docs.python.org/2/library/subprocess.html#subprocess.Popen.wait

  p11 = subprocess.Popen('gnome-terminal -e "perl /tmp/expect',shell=False)  
  p11.wait()

使用pexpect,一个python expect implementation

import pexpect
...
child = pexpect.spawn(cmd) # the command you want to run
child.expect(str_expect) # the string you expect 
child.sendline(str_response) # the string with which you'd like to respond 
child.wait() # wait for the child to exit
...
# the rest of your code here