Daemon for Linux, in Python

Klaus Alexander Seistrup spam at magnetic-ink.dk
Wed Feb 26 13:32:04 EST 2003


Benne wrote:

> Is it possible to bould an system-service / deamon in python?

Perhaps you could just do something along these lines:

#v+

def daemonize(home='/'):
    import sys, os
    for fd in range(255, 2, -1):
        try:
            os.close(fd)
        except:
            pass
        # end try
    # end for
    nullfp = lambda mode: open('/dev/null', mode)
    sys.stderr.close()
    sys.stderr = nullfp('w')
    sys.stdout.close()
    sys.stdout = nullfp('w')
    sys.stdin.close()
    sys.stdin  = nullfp('r')
    pid = os.fork()
    if pid != 0:
        os._exit(0)
    # end if
    os.setsid()
    os.chdir(home)
    #
    pid = os.fork()
    if pid != 0:
        os._exit(0)
    # end if
# end def daemonize

#v-


  // Klaus

-- 
 ><> 	vandag, môre, altyd saam




More information about the Python-list mailing list