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]
log[R]

Public Class Methods

new() click to toggle source
    # File lib/daemon/Daemon.rb, line 26
26:     def initialize
27:       # You can set this flag to false to prevent the program from
28:       # disconnecting from the current terminal. This is useful for debugging
29:       # purposes.
30:       @daemonize = true
31:       @log = LogFile.instance
32:     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 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
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 75
75:     def stop
76:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.