Parent

Methods

Class Index [+]

Quicksearch

TaskJuggler::Daemon

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.

Attributes

daemonize[RW]

Public Class Methods

new() click to toggle source
    # File lib/daemon/Daemon.rb, line 24
24:     def initialize
25:       # You can set this flag to false to prevent the program from
26:       # disconnecting from the current terminal. This is useful for debugging
27:       # purposes.
28:       @daemonize = true
29:       @log = LogFile.instance
30:     end

Public Instance Methods

start() click to toggle source

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
stop() click to toggle source

This method may provide some cleanup functionality in the future. You better call it before you exit.

    # File lib/daemon/Daemon.rb, line 73
73:     def stop
74:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.