Class: Smith::Daemon
Instance Method Summary collapse
-
#daemonise ⇒ Object
Daemonise the process if the daemonise option is true, otherwise do nothing.
-
#initialize(name, daemonise, dir = nil) ⇒ Daemon
constructor
A new instance of Daemon.
-
#running? ⇒ Boolean
Check to see if the program is running.
- #unlink_pid_file ⇒ Object
Methods included from Logger
Constructor Details
#initialize(name, daemonise, dir = nil) ⇒ Daemon
Returns a new instance of Daemon.
11 12 13 14 15 |
# File 'lib/smith/daemon.rb', line 11 def initialize(name, daemonise, dir=nil) @name = name @daemonise = daemonise @pid = Daemons::PidFile.new(pid_dir(dir), @name) end |
Instance Method Details
#daemonise ⇒ Object
Daemonise the process if the daemonise option is true, otherwise do nothing.
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 |
# File 'lib/smith/daemon.rb', line 18 def daemonise unlink_pid_file if @daemonise fork && exit unless Process.setsid raise RuntimeException, 'cannot detach from controlling terminal' end $0 = @name # Be nice to unmount. Dir.chdir "/" STDIN.reopen("/dev/null") STDOUT.reopen("/dev/null") STDERR.reopen(STDOUT) end $0 = @name @pid.pid = Process.pid logger.debug { "Pid file: #{@pid.filename}" } end |
#running? ⇒ Boolean
Check to see if the program is running. This checks for the existance of a pid file and if there is checks to see if the pid exists.
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/smith/daemon.rb', line 46 def running? pid_files = Daemons::PidFile.find_files(@pid.dir, @name) if pid_files.empty? false else pid = File.read(pid_files.first).to_i pid > 0 && Daemons::Pid.running?(pid) end end |
#unlink_pid_file ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/smith/daemon.rb', line 57 def unlink_pid_file p = Pathname.new(@pid.filename) if p.exist? logger.verbose { "Removing pid file." } p.unlink end end |