#NEWLINE# def daemonize( redirect_out = True ) : #NEWLINE# #INDENT# '\n    Daemonize a process\n    ' #NEWLINE# try : #NEWLINE# #INDENT# pid = os . fork ( ) #NEWLINE# if ( pid > 0 ) : #NEWLINE# #INDENT# reinit_crypto ( ) #NEWLINE# sys . exit ( salt . defaults . exitcodes . EX_OK ) #NEWLINE# #UNINDENT# #UNINDENT# except OSError as exc : #NEWLINE# #INDENT# log . error ( 'fork #1 failed: {0} ({1})' . format ( exc . errno , exc . strerror ) ) #NEWLINE# sys . exit ( salt . defaults . exitcodes . EX_GENERIC ) #NEWLINE# #UNINDENT# os . chdir ( '/' ) #NEWLINE# os . setsid ( ) #NEWLINE# os . umask ( 18 ) #NEWLINE# try : #NEWLINE# #INDENT# pid = os . fork ( ) #NEWLINE# if ( pid > 0 ) : #NEWLINE# #INDENT# reinit_crypto ( ) #NEWLINE# sys . exit ( salt . defaults . exitcodes . EX_OK ) #NEWLINE# #UNINDENT# #UNINDENT# except OSError as exc : #NEWLINE# #INDENT# log . error ( 'fork #2 failed: {0} ({1})' . format ( exc . errno , exc . strerror ) ) #NEWLINE# sys . exit ( salt . defaults . exitcodes . EX_GENERIC ) #NEWLINE# #UNINDENT# reinit_crypto ( ) #NEWLINE# if redirect_out : #NEWLINE# #INDENT# with fopen ( '/dev/null' , 'r+' ) as dev_null : #NEWLINE# #INDENT# os . dup2 ( dev_null . fileno ( ) , sys . stdin . fileno ( ) ) #NEWLINE# os . dup2 ( dev_null . fileno ( ) , sys . stdout . fileno ( ) ) #NEWLINE# os . dup2 ( dev_null . fileno ( ) , sys . stderr . fileno ( ) )