def daemonize(pidfile=None, uid=None, umask=63):
 if os.fork():
  return 1
 os.setsid()
 if os.fork():
  return 2
 os.chdir('/')
 null = os.open('/dev/null', os.O_RDWR)
 for i in range(3):
  try:
   os.dup2(null, i)
  except OSError as e:
   if (e.errno != errno.EBADF):
    raise
 os.close(null)
 warnings.filterwarnings('ignore')
 if pidfile:
  piddir = os.path.dirname(pidfile)
  if (not os.path.exists(piddir)):
   os.makedirs(piddir)
  f = file(pidfile, 'w')
  f.write(str(os.getpid()))
  f.close()
 if (umask is not None):
  os.umask(umask)
 if uid:
  setugid(uid)
 return None

def daemonize ( pidfile = None , uid = None , umask = 63 ) : if os . fork ( ) : return 1 os . setsid ( ) if os . fork ( ) : return 2 os . chdir ( '/' ) null = os . open ( '/dev/null' , os . O_RDWR ) for i in range ( 3 ) : try : os . dup2 ( null , i ) except OSError as e : if ( e . errno != errno . EBADF ) : raise os . close ( null ) warnings . filterwarnings ( 'ignore' ) if pidfile : piddir = os . path . dirname ( pidfile ) if ( not os . path . exists ( piddir ) ) : os . makedirs ( piddir ) f = file ( pidfile , 'w' ) f . write ( str ( os . getpid ( ) ) ) f . close ( ) if ( umask is not None ) : os . umask ( umask ) if uid : setugid ( uid ) return None
daemonize a unix process .