【发布时间】:2015-08-12 13:24:40
【问题描述】:
我尝试在 python 中创建一个备份脚本并使用 popen 启动、停止服务... 停止服务正在工作,但不幸的是启动服务工作,但阻止了其余的执行,脚本停留在那里,为什么?
似乎以某种方式与 httpd 服务相关联... :-(
程序配置元素类似于“service;httpd;start”或“/etc/init.d/myprog;start”
class execute(actions):
def __init__(self,config,section,logger):
self.name="execute"
actions.__init__(self,config,section,logger)
def process(self):
try:
program=self.config.get(self.section,"program").split(";")
self.logger.debug("program=%s" % program)
p = subprocess.Popen(program, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
if stdout:
self.logger.info(stdout)
if stderr:
self.logger.error(stderr)
return p.returncode
except Exception:
self.logger.exception(Exception)
【问题讨论】:
-
嗯,当你在命令行中输入
service myprog start会发生什么?您是否必须等到在另一个终端输入service myprog stop? -
不,很快,大约 5 秒
-
我也想知道为什么 stdout 只在命令结束时显示...我在运行时看不到它
标签: python python-2.7 subprocess popen