Python and Daemons

Hunter Matthews thm at duke.edu
Wed Feb 27 17:49:59 CET 2002


Justin Sheehy <justin at iago.org> wrote in message news:<mailman.1014416559.28231.python-list at python.org>...
> 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

Can someone say why the 2nd fork is widely recommneded?



More information about the Python-list mailing list