Class: Daemons::PidFile

Inherits:
Pid
  • Object
show all
Defined in:
lib/daemons/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.

Daemons needs the pid of the scripts that are currently running in the background to send them so called signals. Daemons 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 Daemons have to following format:

<scriptname>.rb<number>.pid

(Note that <number> is omitted if only one instance of the script can run at any time)

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

Where are the Pid-Files stored?

Daemons is configurable to store the Pid-Files relative to three different locations:

  1. in a directory relative to the directory where the script (the one that is supposed to run as a daemon) resides (:script option for :dir_mode)

  2. in a directory given by :dir (:normal option for :dir_mode)

  3. in the preconfigured directory /var/run (:system option for :dir_mode)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Pid

dir, running?, #running?

Constructor Details

#initialize(dir, progname, multiple = false) ⇒ PidFile

Returns a new instance of PidFile.


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/daemons/pidfile.rb', line 65

def initialize(dir, progname, multiple = false)
  @dir = File.expand_path(dir)
  @progname = progname
  @multiple = multiple
  @number = nil
  @number = 0 if multiple

  if multiple
    while File.exist?(filename) && @number < 1024
      @number += 1
    end

    if @number >= 1024
      fail RuntimeException('cannot run more than 1024 instances of the application')
    end
  end
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir


31
32
33
# File 'lib/daemons/pidfile.rb', line 31

def dir
  @dir
end

#multipleObject (readonly)

Returns the value of attribute multiple


31
32
33
# File 'lib/daemons/pidfile.rb', line 31

def multiple
  @multiple
end

#numberObject (readonly)

Returns the value of attribute number


31
32
33
# File 'lib/daemons/pidfile.rb', line 31

def number
  @number
end

#prognameObject (readonly)

Returns the value of attribute progname


31
32
33
# File 'lib/daemons/pidfile.rb', line 31

def progname
  @progname
end

Class Method Details

.existing(path) ⇒ Object


53
54
55
56
57
58
59
60
61
62
63
# File 'lib/daemons/pidfile.rb', line 53

def self.existing(path)
  new_instance = PidFile.allocate

  new_instance.instance_variable_set(:@path, path)

  def new_instance.filename
    @path
  end

  new_instance
end

.find_files(dir, progname, delete = false) ⇒ Object


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/daemons/pidfile.rb', line 33

def self.find_files(dir, progname, delete = false)
  files = Dir[File.join(dir, "#{progname}_num*.pid")]
  files = Dir[File.join(dir, "#{progname}.pid")] if files.size == 0

  files.delete_if { |f| not (File.file?(f) and File.readable?(f)) }
  if delete
    files.delete_if do |f|
      pid = File.open(f) { |h| h.read }.to_i
      rsl =  !Pid.running?(pid)
      if rsl
        puts "pid-file for killed process #{pid} found (#{f}), deleting."
        begin; File.unlink(f); rescue ::Exception; end
      end
      rsl
    end
  end

  files
end

Instance Method Details

#cleanupObject


98
99
100
# File 'lib/daemons/pidfile.rb', line 98

def cleanup
  File.delete(filename) if pid == Process.pid
end

#exist?Boolean

Returns:

  • (Boolean)

87
88
89
# File 'lib/daemons/pidfile.rb', line 87

def exist?
  File.exist? filename
end

#filenameObject


83
84
85
# File 'lib/daemons/pidfile.rb', line 83

def filename
  File.join(@dir, "#{@progname}#{@number ? '_num' + @number.to_s : '' }.pid")
end

#pidObject


102
103
104
105
106
107
108
109
110
111
112
# File 'lib/daemons/pidfile.rb', line 102

def pid
  begin
    File.open(filename) do |f|
      p = f.gets.to_i
      return nil if p == 0 # Otherwise an invalid pid file becomes pid 0
      return p
    end
  rescue ::Exception
    return nil
  end
end

#pid=(p) ⇒ Object


91
92
93
94
95
96
# File 'lib/daemons/pidfile.rb', line 91

def pid=(p)
  File.open(filename, 'w') do |f|
    f.chmod(0644)
    f.puts p   # Process.pid
  end
end