问题描述
根据之前的后,脚本现在可以正确地启动和停止python脚本(并且仅适用于该特定脚本),但不会在屏幕上报告确定 ...
Following on from the previous post, the script now start and stops the python script (and only that particular script) correctly but does not report the OK back to the screen...
USER="root"
APPNAME="myPythonApp1"
APPBIN="/usr/bin/python"
APPARGS="/usr/local/sbin/app1/app.py"
LOGFILE="/var/log/$APPNAME/error.log"
LOCKFILE="/var/lock/subsys/$APPNAME"
LOGPATH=$(dirname $LOGFILE)
prog=$APPBIN
start() {
[ -x $prog ] || exit 5
[ -d $LOGPATH ] || mkdir $LOGPATH
[ -f $LOGFILE ] || touch $LOGFILE
echo -n $"Starting $APPNAME: "
daemon --user=$USER "$APPBIN $APPARGS >>$LOGFILE &"
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch $LOCKFILE
return $RETVAL
}
stop() {
echo -n $"Stopping $APPNAME: "
pid=`ps -ef | grep "[p]ython $APPARGS" | awk '{ print $2 }'`
echo $pid
kill $pid
sleep 1
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $LOCKFILE
return $RETVAL
}
开始:
停止:
我添加的app.py:
Within the app.py I have added:
[...]
def set_exit_handler(func):
signal.signal(signal.SIGTERM, func)
[...]
if __name__ == '__main__':
def on_exit(sig, func=None):
#print "exit handler triggered"
sys.exit(1)
set_exit_handler(on_exit)
在命令行中,我得到了打印(未注释时),但是在守护程序脚本中,我什么也没得到。。。
At command line I get the print (when uncommented) but within the daemon script I get nothing... something is not going back to RETVAL... is it fixable?
有一个(感谢@robert)关于只能将killproc与守护程序一起使用来达到此目的?
There is a post (thanks @robert) about being able only to use killproc with daemons to have this behaviour?
谢谢!
推荐答案
无法与退出处理程序配合使用,所以我结束了而不是使用.pid文件来做...
Couldn't get it to work with the exit handlers so I ended up doing it with .pid files instead...
USER="root"
APPNAME="myPythonApp1"
APPBIN="/usr/bin/python"
APPARGS="/usr/local/sbin/app1/app.py"
LOGFILE="/var/log/$APPNAME/error.log"
LOCKFILE="/var/lock/subsys/$APPNAME"
LOGPATH=$(dirname $LOGFILE)
prog=$APPBIN
start() {
[ -x $prog ] || exit 5
[ -d $LOGPATH ] || mkdir $LOGPATH
[ -f $LOGFILE ] || touch $LOGFILE
echo -n $"Starting $APPNAME: "
daemon --user=$USER "$APPBIN $APPARGS >>$LOGFILE &"
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch $LOCKFILE
return $RETVAL
}
stop() {
echo -n $"Stopping $APPNAME: "
pid=`ps -ef | grep "[p]ython $APPARGS" | awk '{ print $2 }'`
killproc -p /var/run/$APPNAME.pid
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $LOCKFILE
return $RETVAL
}
并且在python中代码:
and within the python code:
if __name__ == '__main__':
pid = str(os.getpid())
pidfile = "/var/run/myPythonApp1.pid"
if os.path.isfile(pidfile):
print "%s already exists" % pidfile
#sys.exit()
else:
file(pidfile, 'w').write(pid)
这篇关于修改python守护程序脚本,stop不会返回OK(但会终止进程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!