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"?
python linux service daemon
tauran Jan 16 '11 at 13:20 2011-01-16 13:20
source share