小编Ron*_*nzo的帖子

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

我目前正在编写我的第一个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 + …
Run Code Online (Sandbox Code Playgroud)

python linux bash subprocess daemon

5
推荐指数
1
解决办法
1145
查看次数

标签 统计

bash ×1

daemon ×1

linux ×1

python ×1

subprocess ×1