Python and Daemons

Justin Sheehy justin at iago.org
Fri Feb 22 17:20:54 EST 2002


Dieter Lunn <theguru at mb.sympatico.ca> writes:

> I was wondering how to write a linux daemon using Python.

This is what I use:

class Log_out:
    '''file-like dummy device, used for daemonization
    writes are redirected into our logging object'''
    def write(self, s):
        debugmsg(s)

def daemonize():
    "cause this process to becaome a daemon"
    if os.fork() != 0:
        os._exit(0)
    os.setsid()
    if os.fork() != 0:      # 2nd fork is widely recommended
        os._exit(0)         # though not always needed
    debugmsg("daemonized")
    os.chdir("/")   # not strictly necessary, but a good idea
    os.umask(0)
    sys.stdin.close()
    sys.stdout = Log_out()
    sys.stderr = Log_out()

debugmsg is a local function which calls a function in my logging library.

-Justin

 





More information about the Python-list mailing list