Note that there are some explanatory texts on larger screens.

plurals
  1. POPython script as linux service/daemon
    text
    copied!<p>Hallo,</p> <p>I'm trying to let a python script run as service (daemon) on (ubuntu) linux.</p> <p>On the web there exist several solutions like:</p> <p><a href="http://pypi.python.org/pypi/python-daemon/" rel="noreferrer">http://pypi.python.org/pypi/python-daemon/</a></p> <blockquote> <p>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.</p> </blockquote> <p><a href="http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/" rel="noreferrer">http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/</a></p> <p>However as I want to integrate my python script specifically with ubuntu linux my solution is a combination with an init.d script</p> <pre><code>#!/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 </code></pre> <p>and in python:</p> <pre><code>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) </code></pre> <p>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"?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload