class DaemonKit::PidFile

Simple pidfile handling for daemon processes

Public Class Methods

new( path ) click to toggle source
# File lib/daemon_kit/pid_file.rb, line 6
def initialize( path )
  @path = path.to_absolute_path
end

Public Instance Methods

cleanup() click to toggle source
# File lib/daemon_kit/pid_file.rb, line 50
def cleanup
  File.delete( @path ) rescue Errno::ENOENT
end
Also aliased as: zap
ensure_stopped!() click to toggle source
# File lib/daemon_kit/pid_file.rb, line 43
def ensure_stopped!
  if self.running?
    puts "Process already running with id #{self.pid}"
    exit 1
  end
end
exists?() click to toggle source
# File lib/daemon_kit/pid_file.rb, line 10
def exists?
  File.exists?( @path )
end
pid() click to toggle source

Return the pid contained in the pidfile, or nil

# File lib/daemon_kit/pid_file.rb, line 35
def pid
  return nil unless self.exists?

  File.open( @path ) { |f|
    return f.gets.to_i
  }
end
running?() click to toggle source

Returns true if the process is running

# File lib/daemon_kit/pid_file.rb, line 15
def running?
  return false unless self.exists?

  # Check if process is in existence
  # The simplest way to do this is to send signal '0'
  # (which is a single system call) that doesn't actually
  # send a signal
  begin
    Process.kill(0, self.pid)
    return true
  rescue Errno::ESRCH
    return false
  rescue ::Exception   # for example on EPERM (process exists but does not belong to us)
    return true
  #rescue Errno::EPERM
  #  return false
  end
end
write!() click to toggle source
# File lib/daemon_kit/pid_file.rb, line 55
def write!
  File.open( @path, 'w' ) { |f|
    f.puts Process.pid
  }
end
zap()
Alias for: cleanup