Module: Capricorn::Daemon::Controller

Defined in:
lib/capricorn/daemon.rb

Class Method Summary collapse

Class Method Details

.start(daemon, &block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/capricorn/daemon.rb', line 36

def self.start(daemon, &block)
  fork do
    Process.setsid
    exit if fork
    if PidFile.exist?
      puts "Pid file #{PidFile.pid_file} already exists. Not starting."
      exit 1
    end
    Capricorn::Daemon::PidFile.store(Process.pid)
    Dir.chdir Capricorn.system.root
    File.umask 0000
    
    Capricorn::ExceptionHandler.redirect_std
    
    trap("TERM") { daemon.stop; exit }
    at_exit { Capricorn::Daemon::PidFile.destroy if $master }
    at_exit(&block) if block
    daemon.start
  end
  puts "Daemon started."
end

.stopObject



58
59
60
61
62
63
64
65
66
67
# File 'lib/capricorn/daemon.rb', line 58

def self.stop
  if !Capricorn::Daemon::PidFile.exist?
    puts "Pid file not found. Is the daemon started?"
    exit
  end
  pid = Capricorn::Daemon::PidFile.recall
  pid && Process.kill("TERM", pid)
rescue Errno::ESRCH
  puts "Pid file found, but process was not running. The daemon may have died."
end