<s> import os , sys <EOL> def daemonize ( logfile = None ) : <EOL> if os . fork ( ) != <NUM_LIT:0> : <EOL> os . _exit ( <NUM_LIT:0> ) <EOL> os . setsid ( ) <EOL> if os . fork ( ) != <NUM_LIT:0> : <EOL> os . _exit ( <NUM_LIT:0> ) <EOL> os . chdir ( '<STR_LIT:/>' ) <EOL> fd = os . open ( '<STR_LIT>' , os . O_RDWR ) <EOL> os . dup2 ( fd , sys . __stdin__ . fileno ( ) ) <EOL> if logfile != None : <EOL> fake_stdout = file ( logfile , '<STR_LIT:a>' , <NUM_LIT:1> ) <EOL> sys . stdout = fake_stdout <EOL> sys . stderr = fake_stdout <EOL> fd = fake_stdout . fileno ( ) <EOL> os . dup2 ( fd , sys . __stdout__ . fileno ( ) ) <EOL> os . dup2 ( fd , sys . __stderr__ . fileno ( ) ) <EOL> if logfile == None : <EOL> os . close ( fd ) </s>
