def doublefork(pidfile, logfile, cwd, umask): # pragma: nocover
    try:
        if os.fork():
            os._exit(os.EX_OK)
    except OSError as e:
        sys.exit('fork #1 failed: ({}) {}'.format(e.errno, e.strerror))
    os.setsid()
    os.chdir(cwd)
    os.umask(umask)
    try:
        if os.fork():
            os._exit(os.EX_OK)
    except OSError as e:
        sys.exit('fork #2 failed: ({}) {}'.format(e.errno, e.strerror))
    if logfile is not None:
        si = open('/dev/null')
        so = open(logfile, 'a+', 0)
        os.dup2(si.fileno(), 0)
        os.dup2(so.fileno(), 1)
        os.dup2(so.fileno(), 2)
        sys.stdin = si
        sys.stdout = sys.stderr = so
    with open(pidfile, 'w') as f:
        f.write(str(os.getpid()))
Daemonize current process.
dataset/ETHPy150Open SmartTeleMax/iktomi/iktomi/utils/system.py/doublefork