Daemonize?

Oren Tirosh oren-py-l at hishome.net
Sat Sep 7 12:10:37 EDT 2002


On Sat, Sep 07, 2002 at 12:43:51PM -0000, A.M. Kuchling wrote:
> Here's the daemonize function I use; I think Greg Ward originally
> gave it to me.
> 
>     def daemonize (self):
>         # Fork once
>         if os.fork() != 0:
>             os._exit(0)
>         os.setsid()                     # Create new session
>         if os.fork() != 0:      
>             os._exit(0)         
>         os.chdir("/")         
>         os.umask(0)
> 
>         os.close(sys.__stdin__.fileno())
>         os.close(sys.__stdout__.fileno())
>         os.close(sys.__stderr__.fileno())

    devnull = os.open('/dev/null', 0)
    os.dup2(devnull, 0)
    os.dup2(devnull, 1)
    os.dup2(devnull, 2)

This is safer because it prevents these file descriptors from being reused.

	Oren




More information about the Python-list mailing list