Class: MCollective::UnixDaemon

Inherits:
Object
  • Object
show all
Defined in:
lib/mcollective/unix_daemon.rb

Class Method Summary collapse

Class Method Details

.daemonizeObject

Daemonize the current process



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/mcollective/unix_daemon.rb', line 4

def self.daemonize
  fork do
    Process.setsid
    exit if fork
    Dir.chdir('/tmp')
    STDIN.reopen('/dev/null')
    STDOUT.reopen('/dev/null', 'a')
    STDERR.reopen('/dev/null', 'a')

    yield
  end
end

.daemonize_runner(pid = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mcollective/unix_daemon.rb', line 17

def self.daemonize_runner(pid=nil)
  raise "The Unix Daemonizer can not be used on the Windows Platform" if Util.windows?

  UnixDaemon.daemonize do
    if pid
      begin
        File.open(pid, 'w') {|f| f.write(Process.pid) }
      rescue Exception => e
      end
    end

    begin
      runner = Runner.new(nil)
      runner.main_loop
    rescue => e
      Log.warn(e.backtrace)
      Log.warn(e)
    ensure
      File.unlink(pid) if pid && File.exist?(pid)
    end
  end
end