Module: DaemonSpawn
- Defined in:
- lib/daemon-spawn.rb
Overview
Large portions of this were liberally stolen from the ‘simple-daemon’ project at simple-daemon.rubyforge.org/
Defined Under Namespace
Classes: Base
Constant Summary collapse
- VERSION =
'0.2.0'
Class Method Summary collapse
-
.start(daemon, args) ⇒ Object
:nodoc:.
-
.status(daemon) ⇒ Object
:nodoc:.
-
.stop(daemon) ⇒ Object
:nodoc:.
-
.usage(msg = nil) ⇒ Object
:nodoc:.
Class Method Details
.start(daemon, args) ⇒ Object
:nodoc:
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/daemon-spawn.rb', line 15 def self.start(daemon, args) #:nodoc: if !File.writable?(File.dirname(daemon.log_file)) STDERR.puts "Unable to write log file to #{daemon.log_file}" exit 1 end if !File.writable?(File.dirname(daemon.pid_file)) STDERR.puts "Unable to write log file to #{daemon.pid_file}" exit 1 end if daemon.alive? && daemon.singleton STDERR.puts "An instance of #{daemon.classname} is already " + "running (PID #{daemon.pid})" exit 0 end fork do Process.setsid exit if fork open(daemon.pid_file, 'w') { |f| f << Process.pid } Dir.chdir daemon.working_dir File.umask 0000 log = File.new(daemon.log_file, "a") log.sync = daemon.sync_log STDIN.reopen "/dev/null" STDOUT.reopen log STDERR.reopen STDOUT trap("TERM") {daemon.stop; exit} daemon.start(args) end puts "#{daemon.classname} started." end |
.status(daemon) ⇒ Object
:nodoc:
64 65 66 67 68 69 70 |
# File 'lib/daemon-spawn.rb', line 64 def self.status(daemon) #:nodoc: if daemon.alive? puts "#{daemon.classname} is running (PID #{PidFile.recall(daemon)})" else puts "#{daemon.classname} is NOT running" end end |
.stop(daemon) ⇒ Object
:nodoc:
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/daemon-spawn.rb', line 49 def self.stop(daemon) #:nodoc: if pid = daemon.pid FileUtils.rm(daemon.pid_file) Process.kill("TERM", pid) begin Process.wait(pid) rescue Errno::ECHILD end else puts "Pid file not found. Is the daemon started?" end rescue Errno::ESRCH puts "Pid file found, but process was not running. The daemon may have died." end |
.usage(msg = nil) ⇒ Object
:nodoc:
8 9 10 11 12 13 |
# File 'lib/daemon-spawn.rb', line 8 def self.usage(msg=nil) #:nodoc: print "#{msg}, " if msg puts "usage: #{$0} <command> [options]" puts "Where <command> is one of start, stop, restart or status" puts "[options] are additional options passed to the underlying process" end |