Module: SimpleDaemon::Controller
- Defined in:
- lib/simple-daemon.rb
Class Method Summary collapse
Class Method Details
.daemonize(daemon) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/simple-daemon.rb', line 41 def self.daemonize(daemon) case !ARGV.empty? && ARGV[0] when 'start' start(daemon) when 'stop' stop(daemon) when 'restart' stop(daemon) start(daemon) else puts "Invalid command. Please specify start, stop or restart." exit end end |
.start(daemon) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/simple-daemon.rb', line 56 def self.start(daemon) fork do Process.setsid exit if fork if File.file?(daemon.pid_fn) puts "Pid file #{daemon.pid_fn} already exists. Not starting." exit 1 end PidFile.store(daemon, Process.pid) Dir.chdir SimpleDaemon::WORKING_DIRECTORY File.umask 0000 log = File.new("#{daemon.classname}.log", "a") log.sync = true STDIN.reopen "/dev/null" STDOUT.reopen log STDERR.reopen STDOUT trap("TERM") {daemon.stop; exit} daemon.start end puts "Daemon started." end |
.stop(daemon) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/simple-daemon.rb', line 78 def self.stop(daemon) if !File.file?(daemon.pid_fn) puts "Pid file not found. Is the daemon started?" exit end pid = PidFile.recall(daemon) FileUtils.rm(daemon.pid_fn) pid && Process.kill("TERM", pid) rescue Errno::ESRCH puts "Pid file found, but process was not running. The daemon may have died." end |