Saturday, June 19, 2010

Create daemon process in Python

#!/usr/bin/env python
# Filename: daemon.py

# Make the process to run in the background.

__author__ = "Karthikeyan Periasamy"
__version__ = "0.1"
__all__ = ["daemonize"]

# Standard modules
import os               # OS interface functions.
import sys              # System specific functions.
from syslog import *    # Logging functions.

# Daemon parameters
UMASK = 0       # File mode creation mask for the calling process 
WORKDIR = "/"   # The Working directory

# Find the null device /dev/null
if (hasattr(os, "devnull")):
    NULL_DEVICE = os.devnull
else:
    NULL_DEVICE = "/dev/null"
    
# Function
def daemonize (procname):
    openlog (procname, LOG_PID)
    
    try:
        # Fork the child
        syslog (LOG_INFO, "Daemizing")
        syslog (LOG_INFO, "Creating the first child")
        pid = os.fork ()
        
        # Exit from the parent
        if 0 != pid:
            syslog (LOG_INFO, "Parent exiting")
            os._exit (0)

        # Make the child as the leader of the process
        syslog (LOG_INFO, "Child created")
        syslog (LOG_INFO, "Creating a new session")
        os.setsid ()

        # Fork a child again to make sure that no terminal device is connected to it.
        syslog (LOG_INFO, "Creating the second child")
        pid = os.fork ()

        # Exit from the first child
        if 0 != pid:
            syslog (LOG_INFO, "First child exiting")
            os._exit (0)

        # Set the calling process's file mode creation mask
        syslog (LOG_INFO, "Set the calling process's file mode creation mask")
        os.umask (UMASK)

        # Changing working directory to '/' to make sure that
        # the process does not depend on any mounted partition
        # execpt the root of the filesystem
        logmsg = 'Changing working directory to "' + WORKDIR + '" to avoid dependencies'
        syslog (LOG_INFO, logmsg)
        os.chdir (WORKDIR)

        # Close standard input, output and error file descriptors
        syslog (LOG_INFO, "Closing I/O file descriptors")
        os.close (0)
        os.close (1)
        os.close (2)

        # Redirect standard output and errror to /dev/null
        os.open(NULL_DEVICE, os.O_RDWR)
        os.dup2(0, 1)
        os.dup2(0, 2)

    except:
        logmsg = "Unexpected error:" + repr (sys.exc_info()[1])
        syslog (LOG_ERR, logmsg)
        os._exit (1)


# ---------------------------------------------------------------------------
# Main program (for testing)
# ---------------------------------------------------------------------------

if __name__ == '__main__':

    daemonize("daemon_test")
    syslog(LOG_INFO, 'Daemon is sleeping for 10 seconds')

    import time
    for i in range (10):
        syslog(LOG_INFO, 'Sleeping...')
        time.sleep(1)

    syslog(LOG_INFO, 'Daemon exiting')
    sys.exit(0)

Reference: http://code.activestate.com/recipes/278731-creating-a-daemon-the-python-way/