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