Class: Daemonize::PidFile

Inherits:
Object
  • Object
show all
Defined in:
lib/dctl/pidfile.rb

Overview

What is a Pid-File?

A Pid-File is a file containing the process identification number (pid) that is stored in a well-defined location of the filesystem thus allowing other programs to find out the pid of a running script.

Dctl needs the pid of the scripts that are currently running in the background to send them so called signals. Dctl uses the TERM signal to tell the script to exit when you issue a stop command.

How does a Pid-File look like?

Pid-Files generated by Dctl have to following format:

<basename>.pid

This file just contains one line with the pid as string (for example 6432).

Where are Pid-Files stored?

Dctl stores the Pid-files relative to two different locations:

  1. if you are root, Pid-Files are stored to /var/run.

  2. else, Pid-File are stored in the current directory.

Instance Method Summary collapse

Constructor Details

#initialize(path, name) ⇒ PidFile

Returns a new instance of PidFile.

Raises:

  • (Errno::EACCES)


28
29
30
31
32
33
# File 'lib/dctl/pidfile.rb', line 28

def initialize(path, name)
  # Test if this directory is valid.
  Dir.new path
  raise Errno::EACCES, path unless File.writable? path and File.readable? path
  @pathname = File.join(File.expand_path(path), name) + '.pid'
end

Instance Method Details

#deleteObject



46
47
48
49
# File 'lib/dctl/pidfile.rb', line 46

def delete
  File::delete @pathname
  self
end

#readObject



35
36
37
# File 'lib/dctl/pidfile.rb', line 35

def read
  open(@pathname) { |f| Integer(f.read) }
end

#writeObject



39
40
41
42
43
44
# File 'lib/dctl/pidfile.rb', line 39

def write
  f = File.new(@pathname, File::CREAT | File::EXCL | File::WRONLY)
  f << $$
  f.close
  self
end