def daemonize(pidfile=None):
    """
    Turn the running process into a proper daemon according to PEP3143.

    Args:
    pidfile --The pidfile to create.

    """

    # Prevent core dumps
    resource.setrlimit(resource.RLIMIT_CORE, (0, 0))

    # Change working directory
    os.chdir("/")

    # Change file creation mask
    os.umask(0)

    # Detach process context: do double fork
    pid = os.fork()
    if pid > 0:
        os._exit(0)
    os.setsid()
    pid = os.fork()
    if pid > 0:
        os._exit(0)

    # Create signal handler for SIGTERM
    def terminate(signal, stack_frame):
        msg = 'Terminating on signal {}'.format(signal)
        logger.info(msg)
        raise SystemExit(msg)
    signal.signal(signal.SIGTERM, terminate)

    # Redirect input/output streams
    streams = [sys.stdin, sys.stdout, sys.stderr]
    for stream in streams:
        devnull = os.open(os.devnull, os.O_RDWR)
        os.dup2(devnull, stream.fileno())

    # Close file descriptors
    for fd in [stream.fileno() for stream in streams]:
        try:
            os.close(fd)
        except OSError as err:
            if err.errno == errno.EBADF:
                # File descriptor was not open
                pass

    # Create pidfile
    if pidfile is None or pidfile.strip() == '':
        logger.debug('Empty pidfile set')
    else:
        pid = os.getpid()
        try:
            with open(pidfile, 'w') as f:
                f.write('{}\n'.format(pid))
                f.close()
        except EnvironmentError:
            logger.error('Failed to create pidfile at {}'.format(pidfile))

        def remove_pid_file():
            os.remove(pidfile)

        atexit.register(remove_pid_file)

    logger.debug('Process daemonized')
def daemonize ( pidfile = None ) : resource . setrlimit ( resource . RLIMIT_CORE , ( 0 , 0 ) ) os . chdir ( "/" ) os . umask ( 0 ) pid = os . fork ( ) if pid > 0 : os . _exit ( 0 ) os . setsid ( ) pid = os . fork ( ) if pid > 0 : os . _exit ( 0 ) def terminate ( signal , stack_frame ) : msg = 'Terminating on signal {}' . format ( signal ) logger . info ( msg ) raise SystemExit ( msg ) signal . signal ( signal . SIGTERM , terminate ) streams = [ sys . stdin , sys . stdout , sys . stderr ] for stream in streams : devnull = os . open ( os . devnull , os . O_RDWR ) os . dup2 ( devnull , stream . fileno ( ) ) for fd in [ stream . fileno ( ) for stream in streams ] : try : os . close ( fd ) except OSError as err : if err . errno == errno . EBADF : pass if pidfile is None or pidfile . strip ( ) == '' : logger . debug ( 'Empty pidfile set' ) else : pid = os . getpid ( ) try : with open ( pidfile , 'w' ) as f : f . write ( '{}\n' . format ( pid ) ) f . close ( ) except EnvironmentError : logger . error ( 'Failed to create pidfile at {}' . format ( pidfile ) ) def remove_pid_file ( ) : os . remove ( pidfile ) atexit . register ( remove_pid_file ) logger . debug ( 'Process daemonized' )
Turn the running process into a proper daemon according to PEP3143.

    Args:
    pidfile --The pidfile to create.