Python script as linux service / daemon

Hello,

I am trying to run a python script as a service (daemon) on (ubuntu) linux.

There are several solutions on the Internet, such as:

http://pypi.python.org/pypi/python-daemon/

A well-thought out Unix daemon process is difficult to work properly, but the required steps are the same for all daemon programs. The DaemonContext instance contains the behavior and customized process environment for the program; use the instance as a context manager to enter the daemon state.

http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/

However, since I want to integrate my python script specifically with ubuntu linux, my solution is a combination with 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 

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) 

Now my question is: is this approach correct. Should I handle any additional signals? Will it be a "well-established Unix daemon process"?

+67
python linux service daemon
Jan 16 '11 at 13:20
source share
2 answers

Assuming your daemon has some way to run continuously (some kind of event loop, twisted, whatever), you could try using upstart .

Here's an example upstart configuration 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 

If you save this as script.conf in /etc/init , you just do one-time

 $ sudo initctl reload-configuration $ sudo start script 

You can stop it with a stop script . What I said above suggests starting this service on reboots, as well as restarting it if it dies.

As for signal processing, your process should respond normally to SIGTERM . By default, this should be handled unless you specifically set up your own signal handler.

+84
Jan 16 2018-11-16T00:
source share

Rloton's answer is good. Here is a slight refinement, simply because I spent a lot of time debugging. And I need to make a new answer so that I can format it correctly.

A few other points that required me to debug:

  • When this fails, first check /var/log/upstart/.log
  • If your script implements a daemon with python-daemon , you are NOT using the 'expect daemon' stanza. Lack of "expected" work. I do not know why. (If anyone knows why - please write!)
  • Also, keep checking the status of the initctl script to make sure you are busy (start / run). (and reboot when updating the conf file)

Here is my version:

 description "My service" author "Some Dude <blah@foo.com>" env PYTHON_HOME=/<pathtovirtualenv> env PATH=$PYTHON_HOME:$PATH start on runlevel [2345] stop on runlevel [016] chdir <directory> # NO expect stanza if your script uses python-daemon exec $PYTHON_HOME/bin/python script.py # Only turn on respawn after you've debugged getting it to start and stop properly respawn 
+8
Feb 14 2018-12-14T00:
source share



All Articles