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
        '''

        def _maxfd(limit=1024):
            ''' Use the getrlimit method to retrieve the maximum file 
            descriptor number that can be opened by this process. If 
            there is not limit on the resource, use the default value
            
            limit - default maximum for the number of available file descriptors.
            '''
            maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
            if maxfd == resource.RLIM_INFINITY:
                return limit
            else:
                return maxfd
        
        def _devnull(default="/dev/null"):
            # The standard I/O file descriptors are redirected to /dev/null by default.
            if hasattr(os, "devnull"):
                return os.devnull
            else:
                return default

        def _close_fds(preserve=None):
            preserve = preserve or []
            for fd in xrange(0, _maxfd()):
                if fd not in preserve:
                    try:
                        os.close(fd)
                    except __HOLE__: # fd wasn't open to begin with (ignored)
                        pass

        pid = self._fork(1) # first fork
        if pid == 0: # the first child
            os.setsid()
            pid = self._fork(2)
            if pid == 0: # the second child
                os.chdir("/") 
                os.umask(0) 
            else:
                os._exit(0)
            _close_fds(logging_file_descriptors())
        else:
            os._exit(0)             

        os.open(_devnull(), os.O_RDWR)
        os.dup2(0, 1)			# standard output (1)
        os.dup2(0, 2)			# standard error (2)

        return True
dataset/ETHPy150Open ownport/pyservice/pyservice.py/Service.daemonize