def daemonize(reactor, os):
    """
    Daemonizes the application on Unix. This is done by the usual double
    forking approach.

    @see: U{http://code.activestate.com/recipes/278731/}
    @see: W. Richard Stevens, "Advanced Programming in the Unix Environment",
          1992, Addison-Wesley, ISBN 0-201-56317-7

    @param reactor: The reactor in use.  If it provides L{IReactorDaemonize},
        its daemonization-related callbacks will be invoked.

    @param os: An object like the os module to use to perform the daemonization.
    """

    ## If the reactor requires hooks to be called for daemonization, call them.
    ## Currently the only reactor which provides/needs that is KQueueReactor.
    if IReactorDaemonize.providedBy(reactor):
        reactor.beforeDaemonize()

    if os.fork():   # launch child and...
        os._exit(0) # kill off parent
    os.setsid()
    if os.fork():   # launch child and...
        os._exit(0) # kill off parent again.
    null = os.open('/dev/null', os.O_RDWR)
    for i in range(3):
        try:
            os.dup2(null, i)
        except __HOLE__, e:
            if e.errno != errno.EBADF:
                raise
    os.close(null)

    if IReactorDaemonize.providedBy(reactor):
        reactor.afterDaemonize()
dataset/ETHPy150Open nlloyd/SubliminalCollaborator/libs/twisted/scripts/_twistd_unix.py/daemonize