This class provides the basic functionality to turn the current process into a background process (daemon). To use it, derive you main class from this class and call the start() method.
Call this method to turn the process into a background process.
# File lib/daemon/Daemon.rb, line 35 35: def start 36: return unless @daemonize 37: 38: # Fork and have the parent exit 39: if (pid = fork) == 1 40: @log.fatal('First fork failed') 41: elsif !pid.nil? 42: # This is the parent. We can exit now. 43: @log.debug("Forked a child process with PID #{pid}") 44: exit 0 45: end 46: 47: # Create a new session 48: Process.setsid 49: # Fork again to make sure we lose the controlling terminal 50: if (pid = fork) == 1 51: @log.fatal('Second fork failed') 52: elsif !pid.nil? 53: # This is the parent. We can exit now. 54: @log.debug("Forked a child process with PID #{pid}") 55: exit 0 56: end 57: 58: @pid = Process.pid 59: 60: # Change current working directory to the file system root 61: Dir.chdir '/' 62: # Make sure we can create files with any permission 63: File.umask 0 64: 65: # We no longer have a controlling terminal, so these are useless. 66: $stdin.reopen('/dev/null') 67: $stdout.reopen('/dev/null', 'a') 68: $stderr.reopen($stdout) 69: 70: @log.info("The process is running as daemon now with PID #{@pid}") 71: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.