【问题标题】:How to kill and restart processes within a pexpect child process?如何在 pexpect 子进程中终止和重新启动进程?
【发布时间】:2017-11-26 09:43:36
【问题描述】:

我目前可以使用pexpect 生成一个 SSH 连接,并从该 SSH 连接中启动另一个进程 (my_app)。像这样:

child = pexpect.spawn('ssh user@example.com')
# ... handle connection details ...
child.sendline('./my_app')
# ... interact with my_app ...

这一切都很好。但是,如果需要,我希望能够杀死 my_app 并重新启动它。我可以通过在后台启动 my_app (./my_app &)、将 PID 保存到变量、发送 CTRL-Z 并发出 kill 命令来做到这一点,但这在很多方面都很不稳定。

还有其他更正确/更稳定的方法吗?

【问题讨论】:

  • 你怎么知道什么时候杀死应用程序?
  • 一旦满足某些条件,通常在一系列步骤结束时,但由于我可能处于应用程序的不同状态,因此杀死它比尝试计算更容易一种将其重置为初始状态的通用方法。

标签: python ssh spawn pexpect


【解决方案1】:

为了杀死子进程(pexpect),我们可以使用close()

child = pexpect.spawn('ssh user@example.com')

child.close()

重启可以再次使用spawn

请参考pexpect — Spawn child applications and control them automatically

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    相关资源
    最近更新 更多