使用nohup在后台调用python脚本会挂起,并且不会继续前进

时间:2019-01-07 07:28:09

标签: python-3.x subprocess background-process

为什么在调用'nohup python3 srvr_script'之后以下脚本没有继续并继续打印出Output / Out / Err / Return语句?而是打印:“ nohup:忽略输入并将stderr重定向到stdout”,并且不再进行任何移动。如何使它在后台运行脚本并继续前进?谢谢,

from subprocess import Popen, PIPE
from subprocess import check_output


class MailServer(object):
    @staticmethod
    def start_server():
        # Find out if script 'server_script' already started. 
        srvr_script = "server_script.py"
        ps_process = Popen(["ps", "-ef"], stdout=PIPE)
        grep_script = Popen(["grep", srvr_script], stdin=ps_process.stdout, stdout=PIPE)
        grep_python = Popen(["grep", "python"], stdin=grep_script.stdout, stdout=PIPE)

        if grep_python.returncode == 0:
            print("Server already started! Do nothing.")
        else:
            print("Server not started yet! Start server.")
            try:
                srv_proc = check_output(["nohup", 
                                        "python3", 
                                        srvr_script])
                out, err = srv_proc.communicate()
                print("Output: {}".format(srv_proc))
                print("Out: {}".format(out))
                print("Err: {}".format(err))
                print("Return code: {}".format(srv_proc.returncode))
            except Exception as script_err:
                print("Error: %s" % script_err)


MailServer().start_server()

0 个答案:

没有答案