Module: Revenant::Manager
Instance Method Summary collapse
-
#close_open_files ⇒ Object
Close anything that is not one of the three standard IO streams.
- #daemonize(name, log_file = nil) ⇒ Object
-
#redirect_io_to(log_file) ⇒ Object
Redirects STDIN, STDOUT, and STDERR to the specified
log_file
or to /dev/null if none is given. -
#reopen_io(io, path, mode = nil) ⇒ Object
Attempts to reopen an IO object.
Instance Method Details
#close_open_files ⇒ Object
Close anything that is not one of the three standard IO streams.
28 29 30 31 32 33 34 35 36 |
# File 'lib/revenant/manager.rb', line 28 def close_open_files ObjectSpace.each_object(IO) do |io| next if [STDIN, STDOUT, STDERR].include?(io) begin io.close unless io.closed? rescue ::Exception end end end |
#daemonize(name, log_file = nil) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/revenant/manager.rb', line 5 def daemonize(name, log_file = nil) # Firstly, get rid of the filthy dirty original process. exit!(0) if fork # Now that we aren't attached to a terminal, we can become # a session leader. begin Process.setsid rescue Errno::EPERM raise SystemCallError, "setsid failed. terminal failed to detach?" end trap 'SIGHUP', 'IGNORE' # don't do anything crazy when this process exits # Finally, time to create a daemonized process exit!(0) if fork $0 = name.to_s # set the process name close_open_files redirect_io_to(log_file) srand # re-seed the PRNG with our 'final' pid end |
#redirect_io_to(log_file) ⇒ Object
Redirects STDIN, STDOUT, and STDERR to the specified log_file
or to /dev/null if none is given.
40 41 42 43 44 45 46 |
# File 'lib/revenant/manager.rb', line 40 def redirect_io_to(log_file) log_file ||= "/dev/null" reopen_io STDIN, "/dev/null" reopen_io STDOUT, log_file, "a" reopen_io STDERR, STDOUT STDERR.sync = STDOUT.sync = true end |
#reopen_io(io, path, mode = nil) ⇒ Object
Attempts to reopen an IO object.
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/revenant/manager.rb', line 49 def reopen_io(io, path, mode = nil) begin if mode io.reopen(path, mode) else io.reopen(path) end io.binmode rescue ::Exception end end |