Python服务 /守护程序



我正在编写一个我需要在启动时启动的python脚本(一旦启动,它应该继续运行)。我希望能够通过运行一个命令来管理服务:

sudo service my-service (and either start, stop, restart, etc.)

我一直在阅读很多内容,并且真的很想完成我的项目。我看到了这一点,但是如果它可以使用Linux启动脚本,则需要实现它。我什至不确定从哪里开始,我真的不知道如何在bash中编程,但我对所有解决方案都开放。提前感谢您,我感谢所有回复!

看一下zdaemon。它提供了一种简单的方法来训练Python过程。

然后,您可以根据OS编写INIT.D脚本。或者,您可以使用Upstart,Supervisord之类的工具来控制守护程序。

我的init.d脚本(在Centos 5.8上)看起来像:

. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
APP_PATH=/path/to/your/app
PYTHON=/usr/local/bin/python
USER=user
start() {
    cd $APP_PATH
    zdaemon -C app.zdconf start
}
stop() {
    cd $APP_PATH
    zdaemon -C app.zdconf stop
}
check_status() {
        cd $APP_PATH
        zdaemon -C app.zdconf status
}
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        check_status
        ;;
  restart)
        stop
        start
        ;;
  *)
esac
exit 0

其中app.zdconf是zdaemon配置文件。

最新更新