def daemonize(self):
        # Check PID file
        if os.path.isfile(self.conf['pidfile']):
            lg.error("PID file %s already exists" % self.conf['pidfile'])
            sys.exit(1)

        # Unix double-fork magic
        pid = os.fork()
        if pid:
            sys.exit(0)

        os.chdir('/')
        os.setsid()
        os.umask(0)

        pid = os.fork()
        if pid:
            sys.exit(0)

        # Ensure that directories for stdout and stderr logging
        # exists or create them
        for log in ['stdout', 'stderr', 'stdin']:
            path = os.path.dirname(self.conf[log])
            if not os.path.exists(path):
                os.mkdir(path)

        self._redirect_standard_io()

        # Save PID into pidfile
        try:
            fh = open(self.conf['pidfile'], 'w')
            fh.write(str(os.getpid()))
            fh.flush()
            fh.close()
        except Exception as e:
            lg.error("Can't write PID into pidfile: %s" % e)
            sys.exit(1)

        try:
            self.run()
        except Exception as e:
            # On exception, try to shutdown and log
            self._shutdown(exitcode=1)
            lg.exception(e)
Daemonize and run daemon
dataset/ETHPy150Open gooddata/smoker/smoker/server/daemon.py/Smokerd.daemonize