Python 脚本作为 linux 服务/守护进程 [英] Python script as linux service/daemon
问题描述
你好,
我试图让 python 脚本作为服务(守护进程)在 (ubuntu) linux 上运行.
I'm trying to let a python script run as service (daemon) on (ubuntu) linux.
在网络上有几种解决方案,例如:
On the web there exist several solutions like:
http://pypi.python.org/pypi/python-daemon/
一个行为良好的 Unix 守护进程很难做到正确,但每个守护程序所需的步骤都大同小异.DaemonContext 实例保存程序的行为和配置的进程环境;将该实例用作上下文管理器以进入守护程序状态.
A well-behaved Unix daemon process is tricky to get right, but the required steps are much the same for every daemon program. A DaemonContext instance holds the behaviour and configured process environment for the program; use the instance as a context manager to enter a daemon state.
http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/
但是,因为我想将我的 python 脚本专门与 ubuntu linux 集成,所以我的解决方案是与 init.d 脚本相结合
However as I want to integrate my python script specifically with ubuntu linux my solution is a combination with an init.d script
#!/bin/bash
WORK_DIR="/var/lib/foo"
DAEMON="/usr/bin/python"
ARGS="/opt/foo/linux_service.py"
PIDFILE="/var/run/foo.pid"
USER="foo"
case "$1" in
start)
echo "Starting server"
mkdir -p "$WORK_DIR"
/sbin/start-stop-daemon --start --pidfile $PIDFILE
--user $USER --group $USER
-b --make-pidfile
--chuid $USER
--exec $DAEMON $ARGS
;;
stop)
echo "Stopping server"
/sbin/start-stop-daemon --stop --pidfile $PIDFILE --verbose
;;
*)
echo "Usage: /etc/init.d/$USER {start|stop}"
exit 1
;;
esac
exit 0
在 python 中:
and in python:
import signal
import time
import multiprocessing
stop_event = multiprocessing.Event()
def stop(signum, frame):
stop_event.set()
signal.signal(signal.SIGTERM, stop)
if __name__ == '__main__':
while not stop_event.is_set():
time.sleep(3)
我现在的问题是这种方法是否正确.我是否必须处理任何额外的信号?它会是一个行为良好的 Unix 守护进程"吗?
My question now is if this approach is correct. Do I have to handle any additional signals? Will it be a "well-behaved Unix daemon process"?
推荐答案
假设你的守护进程有某种持续运行的方式(一些事件循环、扭曲等等),你可以尝试使用 upstart
.
Assuming your daemon has some way of continually running (some event loop, twisted, whatever), you can try to use upstart
.
这是一个假设的 Python 服务的示例 upstart 配置:
Here's an example upstart config for a hypothetical Python service:
description "My service"
author "Some Dude <blah@foo.com>"
start on runlevel [234]
stop on runlevel [0156]
chdir /some/dir
exec /some/dir/script.py
respawn
如果你把它保存为 script.conf 到 /etc/init
你就简单地做一次
If you save this as script.conf to /etc/init
you simple do a one-time
$ sudo initctl reload-configuration
$ sudo start script
您可以使用 stop script
停止它.上面的 upstart conf 说的是在重启时启动这个服务,如果它死了也重启它.
You can stop it with stop script
. What the above upstart conf says is to start this service on reboots and also restart it if it dies.
至于信号处理 - 您的进程应该自然地响应 SIGTERM
.默认情况下,除非您专门安装了自己的信号处理程序,否则应处理此问题.
As for signal handling - your process should naturally respond to SIGTERM
. By default this should be handled unless you've specifically installed your own signal handler.
这篇关于Python 脚本作为 linux 服务/守护进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!