Python脚本作为Linux服务/守护程序 [英] Python script as linux service/daemon

查看:195
本文介绍了Python脚本作为Linux服务/守护程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我试图让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中的

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服务的新贵配置示例:

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停止它.上面的新贵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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆