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.3.0'

Class Method Summary collapse

Class Method Details

.alive?(pid) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
# File 'lib/daemon_spawn.rb', line 15

def self.alive?(pid)
  Process.kill 0, pid
rescue Errno::ESRCH
  false
end

.start(daemon, args) ⇒ Object

:nodoc:



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
48
49
50
51
52
53
54
# File 'lib/daemon_spawn.rb', line 21

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 PID file to #{daemon.pid_file}"
    exit 1
  end

  if daemon.alive? && daemon.singleton
    STDERR.puts "An instance of #{daemon.app_name} 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
    old_umask = File.umask 0000
    log = File.new(daemon.log_file, "a")
    File.umask old_umask
    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.app_name} started."
end

.status(daemon) ⇒ Object

:nodoc:



82
83
84
# File 'lib/daemon_spawn.rb', line 82

def self.status(daemon) #:nodoc:
  puts "#{daemon.app_name} is #{daemon.alive? ? "" : "NOT "}running (PID #{daemon.pid})"
end

.stop(daemon) ⇒ Object

:nodoc:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/daemon_spawn.rb', line 56

def self.stop(daemon) #:nodoc:
  if pid = daemon.pid
    FileUtils.rm(daemon.pid_file)
    Process.kill(daemon.signal, pid)
    begin
      Process.wait(pid)
    rescue Errno::ECHILD
    end
    if ticks = daemon.timeout
      while ticks > 0 and alive?(pid) do
        puts "Process is still alive. #{ticks} seconds until I kill -9 it..."
        sleep 1
        ticks -= 1
      end
      if alive?(pid)
        puts "Process didn't quit after timeout of #{daemon.timeout} seconds. Killing..."
        Process.kill 9, pid
      end
    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