Python:daemon
lastmod 2018-08-17created 2016-06-247 edits
PEP-3143
PEP-3143 quotes Richard Stevens's Unix Network Programming and lists the requirements of a unix daemon as follows.
- Close all open File descriptors.
- Change the current working directory
- Reset the file creation mask
- Run in the background
- Disassociate from the process group
- Ignore terminal I/O signals
- Disassociate from the control terminal
- Correctly handle the following situations
- Started by the System V init process.
- Terminated by the SIGTERM signal
- Child processes generate the SIGCLD signal
How to install
Example
- [Recommended] CODE - PYTHON daemon example
# To kick off the script, run the following from the python directory:
# PYTHONPATH=`pwd` python testdaemon.py start
#standard python libs
import logging
import time
#third party libs
from daemon import runner
class App():
def __init__(self):
self.stdin_path = '/dev/null'
self.stdout_path = '/dev/tty'
self.stderr_path = '/dev/tty'
self.pidfile_path = '/var/run/testdaemon/testdaemon.pid'
self.pidfile_timeout = 5
def run(self):
while True:
#Main code goes here ...
#Note that logger level needs to be set to logging.DEBUG before this shows up in the logs
logger.debug("Debug message")
logger.info("Info message")
logger.warn("Warning message")
logger.error("Error message")
time.sleep(10)
app = App()
logger = logging.getLogger("DaemonLog")
logger.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler = logging.FileHandler("/var/log/testdaemon/testdaemon.log")
handler.setFormatter(formatter)
logger.addHandler(handler)
daemon_runner = runner.DaemonRunner(app)
#This ensures that the logger file handle does not get closed during daemonization
daemon_runner.daemon_context.files_preserve=[handler.stream]
daemon_runner.do_action()
/etc/init.d/testdaemon:
#! /bin/bash
# Copyright (c) 1996-2012 My Company.
# All rights reserved.
#
# Author: Bob Bobson, 2012
#
# Please send feedback to [email protected]
#
# /etc/init.d/testdaemon
#
### BEGIN INIT INFO
# Provides: testdaemon
# Required-Start:
# Should-Start:
# Required-Stop:
# Should-Stop:
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Short-Description: Test daemon process
# Description: Runs up the test daemon process
### END INIT INFO
# Activate the python virtual environment
. /path_to_virtualenv/activate
case "$1" in
start)
echo "Starting server"
# Start the daemon
python /usr/share/testdaemon/testdaemon.py start
;;
stop)
echo "Stopping server"
# Stop the daemon
python /usr/share/testdaemon/testdaemon.py stop
;;
restart)
echo "Restarting server"
python /usr/share/testdaemon/testdaemon.py restart
;;
*)
# Refuse to do other stuff
echo "Usage: /etc/init.d/testdaemon.sh {start|stop|restart}"
exit 1
;;
esac
exit 0
See also
Favorite site
- Making a Daemon with Python
- python daemon sample
- Stackoverflow: Python script as linux service/daemon
- Stackoverflow: How do I set up a daemon with python-daemon?
Other implementation
Pages that link here