lib/daemons/daemonize.rb in daemons-1.0.1 vs lib/daemons/daemonize.rb in daemons-1.0.2

- old
+ new

@@ -111,11 +111,11 @@ end module_function :safefork def simulate(logfile_name = nil) - # NOTE: STDOUT and STDERR will not be redirected to the logfile! + # NOTE: STDOUT and STDERR will not be redirected to the logfile, because in :ontop mode, we normally want to see the output Dir.chdir "/" # Release old working directory File.umask 0000 # Insure sensible umask # Make sure all file descriptors are closed @@ -137,11 +137,11 @@ STDIN.reopen "/dev/null" rescue nil end module_function :simulate - def call_as_daemon(block, logfile_name = nil, oldmode = 0) + def call_as_daemon(block, logfile_name = nil, app_name = nil) rd, wr = IO.pipe if tmppid = safefork # parent wr.close @@ -150,26 +150,30 @@ Process.waitpid(tmppid) return pid else + # child + rd.close # Detach from the controlling terminal unless sess_id = Process.setsid raise Daemons.RuntimeException.new('cannot detach from controlling terminal') end # Prevent the possibility of acquiring a controlling terminal - if oldmode.zero? + #if oldmode.zero? trap 'SIGHUP', 'IGNORE' exit if pid = safefork - end + #end wr.write Process.pid wr.close + $0 = app_name if app_name + Dir.chdir "/" # Release old working directory File.umask 0000 # Insure sensible umask # Make sure all file descriptors are closed ObjectSpace.each_object(IO) do |io| @@ -208,25 +212,27 @@ end module_function :call_as_daemon # This method causes the current running process to become a daemon - def daemonize(logfile_name = nil, oldmode=0) + def daemonize(logfile_name = nil, app_name = nil) srand # Split rand streams between spawning and daemonized process safefork and exit # Fork and exit from the parent # Detach from the controlling terminal unless sess_id = Process.setsid raise Daemons.RuntimeException.new('cannot detach from controlling terminal') end # Prevent the possibility of acquiring a controlling terminal - if oldmode.zero? + #if oldmode.zero? trap 'SIGHUP', 'IGNORE' exit if pid = safefork - end + #end + $0 = app_name if app_name + Dir.chdir "/" # Release old working directory File.umask 0000 # Insure sensible umask # Make sure all file descriptors are closed ObjectSpace.each_object(IO) do |io| @@ -256,10 +262,11 @@ STDOUT.reopen "/dev/null" rescue nil end STDERR.reopen STDOUT rescue nil - return oldmode ? sess_id : 0 # Return value is mostly irrelevant + #return oldmode ? sess_id : 0 # Return value is mostly irrelevant + return sess_id end module_function :daemonize end