![](/img/trans.png)
[英]terminate/close cmd terminal while program is running using subprocess python
[英]Python: start terminal program and parse its output while it's running
我正在为流行的NodeJS服务器“ MeteorJS”编写Ubuntu的AppIndicator,该应用程序应列出可用项目并可以启动服务器,并且在启动服务器时,它将获取其Terminal输出并对它们做出反应。
当您启动流星时,它会根据发生的情况提供一些输出。 例如,当您更改代码时,它会输出"changed restarting..."
或者当您再次更改"changed restarting... (2x)"
,这很好,但是当出现错误时,则会打印一些错误消息。
除非您的桌面上没有足够的空间来查看该终端,否则这很好。
所以我写了一个应用程序,应该以其他方式通知我这些消息。
我的实际问题:
我需要从python程序启动服务器,同时我可以对服务器在其标准输出上确切写出的输出做出反应。
所以我想
您可能需要研究下面的函数,然后弄清楚如何使其成为“事件驱动”。
但这是我在后台运行bash脚本并在我感兴趣时获取其输出的方式。
# To test it out, run the script and then turn your wifi off and on.
import subprocess
def tail():
command = ["tail", "-f", "/var/log/wifi.log"] # your bash script execute command goes here
popen = subprocess.Popen(command, stdout=subprocess.PIPE)
for line in iter(popen.stdout.readline, ""):
yield line,
logger = tail()
for line in logger:
print line
您可能想要一个pty。 它可以接收来自应用程序的输出,您可以从中读取并根据需要进行处理。
这是一个Python示例。 它只是将所有内容记录到一个文件中并将其发送到终端。 它基本上类似于script(1),但带有可选时间戳的输出文件。 http://stromberg.dnsalias.org/~strombrg/pypty/
HTH
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.