如何启动进程并将其放入python的后台?

Ron*_*nzo 5 python linux bash subprocess daemon

我目前正在编写我的第一个python程序(在Python 2.6.6中).该程序有助于启动和停止在服务器上运行的不同应用程序,这些应用程序提供用户公共命令(例如在Linux服务器上启动和停止系统服务).

我正在启动应用程序的启动脚本

p = subprocess.Popen(startCommand, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, err = p.communicate()
print(output)
Run Code Online (Sandbox Code Playgroud)

问题是,一个应用程序的启动脚本保持在前台,因此p.communicate()会永远等待.我已经尝试在startCommand前使用"nohup startCommand&",但是没有按预期工作.

作为一种解决方法,我现在使用以下bash脚本来调用应用程序的启动脚本:

#!/bin/bash

LOGFILE="/opt/scripts/bin/logs/SomeServerApplicationStart.log"

nohup /opt/someDir/startSomeServerApplication.sh >${LOGFILE} 2>&1 &

STARTUPOK=$(tail -1 ${LOGFILE} | grep "Server started in RUNNING mode" | wc -l)
COUNTER=0

while [ $STARTUPOK -ne 1 ] && [ $COUNTER -lt 100 ]; do
   STARTUPOK=$(tail -1 logs/SomeServerApplicationStart.log | grep "Server started in RUNNING mode" | wc -l)
   if (( STARTUPOK )); then
      echo "STARTUP OK"
      exit 0
   fi
   sleep 1
   COUNTER=$(( $COUNTER + 1 ))
done

echo "STARTUP FAILED"
Run Code Online (Sandbox Code Playgroud)

从我的python代码调用bash脚本.这种解决方法很完美但我宁愿在python中做所有...

是subprocess.Popen错误的方式?我怎么能只用Python来完成我的任务?

Ser*_*sta 2

首先,很容易不阻止Python脚本进行通信...通过不调用通信!只需从命令的输出或错误输出中读取,直到找到正确的消息并忘记该命令。

# to avoid waiting for an EOF on a pipe ...
def getlines(fd):
    line = bytearray()
    c = None
    while True:
        c = fd.read(1)
        if c is None:
            return
        line += c
        if c == '\n':
            yield str(line)
            del line[:]

p = subprocess.Popen(startCommand, shell=True, stdout=subprocess.PIPE,
               stderr=subprocess.STDOUT) # send stderr to stdout, same as 2>&1 for bash
for line in getlines(p.stdout):
    if "Server started in RUNNING mode" in line:
        print("STARTUP OK")
        break
else:    # end of input without getting startup message
     print("STARTUP FAILED")
     p.poll()    # get status from child to avoid a zombie
     # other error processing
Run Code Online (Sandbox Code Playgroud)

上述问题是服务器仍然是 Python 进程的子进程,并且可能会收到不需要的信号,例如 SIGHUP。如果您想使其成为守护进程,则必须首先启动一个接下来启动服务器的子进程。这样,当第一个子进程结束时,调用者可以等待它,并且服务器将获得 PPID 1(由 init 进程采用)。您可以使用多处理模块来简化该部分

代码可能是这样的:

import multiprocessing
import subprocess

# to avoid waiting for an EOF on a pipe ...
def getlines(fd):
    line = bytearray()
    c = None
    while True:
        c = fd.read(1)
        if c is None:
            return
        line += c
        if c == '\n':
            yield str(line)
            del line[:]

def start_child(cmd):
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
                         shell=True)
    for line in getlines(p.stdout):
        print line
        if "Server started in RUNNING mode" in line:
            print "STARTUP OK"
            break
    else:
        print "STARTUP FAILED"

def main():
    # other stuff in program
    p = multiprocessing.Process(target = start_child, args = (server_program,))
    p.start()
    p.join()
    print "DONE"
    # other stuff in program

# protect program startup for multiprocessing module
if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

getlines人们可能想知道,当文件对象本身就是一个一次返回一行的迭代器时,生成器需要什么。问题在于,read当文件未连接到终端时,它会在内部调用读取直到 EOF 的操作。由于它现在连接到 PIPE,因此在服务器结束之前您不会得到任何内容...这不是预期的情况