def daemonize (self):

        '''
            Do the UNIX double-fork magic, see Stevens' 'Advanced
            Programming in the UNIX Environment' for details (ISBN 0201563177)
            http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
        '''

        try:

            pid = os.fork()
            if pid > 0:
                # Exit first parent
                sys.exit(0)

        except OSError, e:

            sys.stderr.write(
                'fork #1 failed: %d (%s)\n' % (e.errno, e.strerror))

            sys.exit(1)

        # Decouple from parent environment
        os.chdir(self.home_dir)
        os.setsid()
        os.umask(self.umask)

        # Do second fork
        try:

            pid = os.fork()
            if pid > 0:
                # Exit from second parent
                sys.exit(0)

        except __HOLE__, e:

            sys.stderr.write(
                'fork #2 failed: %d (%s)\n' % (e.errno, e.strerror))

            sys.exit(1)

        # This block breaks on OS X
        if sys.platform != 'darwin':

            # Redirect standard file descriptors
            sys.stdout.flush()
            sys.stderr.flush()
            si = file(self.stdin, 'r')
            so = file(self.stdout, 'a+')

            if self.stderr:
                se = file(self.stderr, 'a+', 0)
            else:
                se = so

            os.dup2(si.fileno(), sys.stdin.fileno())
            os.dup2(so.fileno(), sys.stdout.fileno())
            os.dup2(se.fileno(), sys.stderr.fileno())

        def sigtermhandler (signum, frame):

            self.daemon_alive = False
            sys.exit()

        if self.use_gevent:

            import gevent
            gevent.reinit()
            gevent.signal(signal.SIGTERM, sigtermhandler, signal.SIGTERM, None)
            gevent.signal(signal.SIGINT, sigtermhandler, signal.SIGINT, None)

        else:

            signal.signal(signal.SIGTERM, sigtermhandler)
            signal.signal(signal.SIGINT, sigtermhandler)

        if self.verbose >= 1:
            print 'Started'

        # Make sure pid file is removed if we quit
        atexit.register(self.delpid)

        # Write pidfile
        pid = str(os.getpid())
        file(self.pidfile, 'w+').write('%s\n' % pid)
dataset/ETHPy150Open leonjza/hogar/hogar/Utils/Daemon.py/Daemon.daemonize