简体   繁体   English

用popen启动服务:命令不停止

[英]start a service with popen : command not stopping

I try to make a backup script in python and start, stop a service with popen... Stopping the service is working, but unfortunatly starting the service works, but blocks the rest of the execution, the scripts stays there, why ? 我尝试使用python创建备份脚本并启动,使用popen停止服务...停止服务正在运行,但是不幸的是启动了服务,但是阻止了其余的执行,脚本停留在那里,为什么?

Seems to be somehow linked with the httpd service... :-( 似乎以某种方式与httpd服务链接... :-(

the program config element is like "service;httpd;start" or "/etc/init.d/myprog;start" 程序配置元素类似于“ 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)

You have to open a stdin as a pipe as well, and then close it (if you use read() and write() instead of communicate()). 您还必须打开一个stdin作为管道,然后将其关闭(如果使用read()和write()而不是communication())。

p = subprocess.Popen(..., stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.stdin.close()
print "Stdout:", p.stdout.read()
print "Stderr:", p.stderr.read()

If it doesn't work, and you don't really need any checks, just close all pipes after call to Popen, what will cause program execution and detachment from pipes. 如果它不起作用,并且您真的不需要任何检查,只需在调用Popen之后关闭所有管道,这将导致程序执行和与管道分离。

Warning: This will make program run as a daemon if it doesn't terminate on its own. 警告:如果程序没有自行终止,它将使程序作为守护程序运行。

After doing this you may call wait() to see whether it'll block as well. 完成此操作后,您可以调用wait()来查看它是否也会阻塞。 And use exitcodes to check for eventual errors. 并使用退出代码检查最终错误。

There are not much of them. 数量不多。 Just service started or not. 是否开始服务。 Sometimes even it returns that service is running, but service crashes. 有时甚至返回服务正在运行,但服务崩溃。

To check whether service script is still running, but without blocking, use: 要检查服务脚本是否仍在运行,但没有阻塞,请使用:

if p.poll()==None: print "Still running"

Else, poll() returns the exit code. 否则,poll()返回退出代码。

This works neatly for starting and stopping a service: 这对于启动和停止服务非常有用:

from subprocess import Popen, PIPE
service = "brltty"
p = Popen(["service", service, "start"], stdin=PIPE, stdout=PIPE, stderr=PIPE)
# Note: using sequence uses shell=0
stdout, stderr = p.communicate()
print "Stdout:", stdout
print "Stderr:", stderr

Don't forget to change start to stop :D :D :D 别忘了更改开始到停止:D:D:D

The call to p.communicate() waits for the process to terminate. p.communicate()的调用等待过程终止。

Refer to: subprocess documentation 请参阅: 子流程文档

Interact with process: Send data to stdin. 与进程交互:将数据发送到stdin。 Read data from stdout and stderr, until end-of-file is reached. 从stdout和stderr读取数据,直到到达文件末尾。 Wait for process to terminate . 等待进程终止 The optional input argument should be a string to be sent to the child process, or None, if no data should be sent to the child. 可选输入参数应该是要发送给子进程的字符串,如果没有数据要发送给子进程,则为None。

You can try to use p.poll() instead. 您可以尝试使用p.poll()代替。 This method doesn't wait for a process to terminate. 此方法不等待进程终止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM