class Servolux::PidFile


pid_file.kill(“HUP”) if pid_file.alive?
pid_file = Servolux::PidFile.new(:name => “test”, :path => “/var/run”)
the program.
From another process we can access this PID file and send a ‘HUP` signal to
pid_file.write
pid_file.filename #=> “/var/run/test.pid”
pid_file = Servolux::PidFile.new(:name => “test”, :path => “/var/run”)
Here is a simple example creating a PID file in the “/var/run” directory.
== Examples
can be sent to the program using the `kill` method.
provided to check if the program associated with the PID is `alive?`. Signals
The PidFile class supports creating and deleting PID files. Methods are
started at the same time.
The file can be used to ensure two instances of the same program are not
programmatically to look up the process ID and send signals to the program.
by the program to indicate that it started successfully. The file can be used
A PID file contains the process ID of a given program. This file can be used
== Details
The PidFile manages the lifecycle of a PID file.
== Synopsis

def alive?

to the process.
is not the case. The status of the process is determined by sending signal 0
Returns `true` if the process is currently running. Returns `false` if this
def alive?
  pid = self.pid
  return if pid.nil?
  Process.kill(0, pid)
  true
rescue Errno::ESRCH, Errno::ENOENT
  false
end

def delete

Raises Errno::EACCESS if you do not have permission to delete the file.
Returns the filename of the deleted file or `nil` if no action was taken.

match, then this method returns `nil` without taking any action.
process PID is the same as the PID stored in the file. If the PIDs do not
Delete the PID file if it exists. This method first checks that the current
def delete
  return unless pid == Process.pid
  fn = filename
  logger.debug "Deleting pid file #{fn.inspect}"
  File.delete fn
  fn
end

def delete!

Raises Errno::EACCESS if you do not have permission to delete the file.
Returns the filename of the deleted file or `nil` if no action was taken.

the current process PID against the PID stored in the file.
Forcibly delete the PID file if it exists. This method does NOT check that
def delete!
  return unless exist?
  fn = filename
  logger.debug "Deleting pid file #{fn.inspect}"
  File.delete fn
  fn
end

def exist?

Returns `true` if the PID file exists. Returns `false` otherwise.
def exist?
  File.exist? filename
end

def filename

Returns the full name of the PID file including path and extension.
def filename
  fn = name.to_s.downcase.tr(" ","_") + ".pid"
  fn = File.join(path, fn) unless path.nil?
  fn
end

def initialize( opts = {} )


:logger - logger for outputting messages
:mode - file permissions mode
:path - path to the PID file location
:name - the name of the program
opts - The options Hash

Create a new PID file instance.
def initialize( opts = {} )
  @name   = opts.fetch(:name, $0)
  @path   = opts.fetch(:path, ".")
  @mode   = opts.fetch(:mode, DEFAULT_MODE)
  @logger = opts.fetch(:logger, Servolux::NullLogger())
  yield self if block_given?
end

def kill( signal = 'INT' )

Returns an Integer or `nil` if an error was encountered.

signal - The signal to send to the process (String or Integer)

number.
send is 'INT' (2). The signal can be given either as a string or a signal
Send a signal the process identified by the PID file. The default signal to
def kill( signal = 'INT' )
  pid = self.pid
  return if pid.nil?
  signal = Signal.list.invert[signal] if signal.is_a?(Integer)
  logger.info "Killing PID #{pid} with #{signal}"
  Process.kill(signal, pid)
rescue Errno::EINVAL
  logger.error "Failed to kill PID #{pid} with #{signal}: " \
               "'#{signal}' is an invalid or unsupported signal number."
  nil
rescue Errno::EPERM
  logger.error "Failed to kill PID #{pid} with #{signal}: " \
               "Insufficient permissions."
  nil
rescue Errno::ESRCH
  logger.error "Failed to kill PID #{pid} with #{signal}: " \
               "Process is deceased or zombie."
  nil
rescue Errno::EACCES => err
  logger.error err.message
  nil
rescue Errno::ENOENT => err
  logger.error "Could not find a PID file at #{pid_file.inspect}. " \
               "Most likely the process is no longer running."
  nil
rescue Exception => err
  unless err.is_a?(SystemExit)
    logger.error "Failed to kill PID #{pid} with #{signal}: #{err.message}"
  end
  nil
end

def pid

exist. If you do not have permission to access the file `nil` is returned.
Returns the numeric PID read from the file or `nil` if the file does not
def pid
  fn = filename
  Integer(File.read(fn).strip) if File.exist?(fn)
rescue Errno::EACCES => err
  logger.error "You do not have access to the PID file at " \
               "#{fn.inspect}: #{err.message}"
  nil
end

def write( pid = Process.pid )

Raises Errno::EACCESS if you do not have permission to write the file.
Returns the filename of PID file.

pid - The process ID to write to the file

process ID.
Writes the given `pid` to the PID file. The `pid` defaults to the current
def write( pid = Process.pid )
  fn = filename
  logger.debug "Writing pid file #{fn.inspect}"
  File.open(fn, 'w', mode) { |fd| fd.write(pid.to_s) }
  fn
end