Class: Daemons::PidFile
- Defined in:
- lib/feed_updater/vendor/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 Pid-Files stored?
Daemons is configurable to store the Pid-Files relative to three different locations:
-
in a directory relative to the directory where the script (the one that is supposed to run as a daemon) resides
-
in a directory relative to the current directory or the filesystem root
-
in the preconfigured directory
/var/run
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
-
#multiple ⇒ Object
readonly
Returns the value of attribute multiple.
-
#number ⇒ Object
readonly
Returns the value of attribute number.
-
#progname ⇒ Object
readonly
Returns the value of attribute progname.
Class Method Summary collapse
Instance Method Summary collapse
- #cleanup ⇒ Object
- #exists? ⇒ Boolean
- #filename ⇒ Object
-
#initialize(dir, progname, multiple = false) ⇒ PidFile
constructor
A new instance of PidFile.
- #pid ⇒ Object
- #pid=(p) ⇒ Object
Methods inherited from Pid
Constructor Details
#initialize(dir, progname, multiple = false) ⇒ PidFile
Returns a new instance of PidFile.
56 57 58 59 60 61 |
# File 'lib/feed_updater/vendor/daemons/pidfile.rb', line 56 def initialize(dir, progname, multiple = false) @dir = File.(dir) @progname = progname @multiple = multiple @number = 0 if multiple end |
Instance Attribute Details
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
34 35 36 |
# File 'lib/feed_updater/vendor/daemons/pidfile.rb', line 34 def dir @dir end |
#multiple ⇒ Object (readonly)
Returns the value of attribute multiple.
34 35 36 |
# File 'lib/feed_updater/vendor/daemons/pidfile.rb', line 34 def multiple @multiple end |
#number ⇒ Object (readonly)
Returns the value of attribute number.
34 35 36 |
# File 'lib/feed_updater/vendor/daemons/pidfile.rb', line 34 def number @number end |
#progname ⇒ Object (readonly)
Returns the value of attribute progname.
34 35 36 |
# File 'lib/feed_updater/vendor/daemons/pidfile.rb', line 34 def progname @progname end |
Class Method Details
.existing(path) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/feed_updater/vendor/daemons/pidfile.rb', line 44 def PidFile.existing(path) new_instance = PidFile.allocate new_instance.instance_variable_set(:@path, path) def new_instance.filename return @path end return new_instance end |
.find_files(dir, progname) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/feed_updater/vendor/daemons/pidfile.rb', line 36 def PidFile.find_files(dir, progname) files = Dir[File.join(dir, "#{progname}*.pid")] files.delete_if {|f| not (File.file?(f) and File.readable?(f))} return files end |
Instance Method Details
#cleanup ⇒ Object
87 88 89 |
# File 'lib/feed_updater/vendor/daemons/pidfile.rb', line 87 def cleanup File.delete(filename) end |
#exists? ⇒ Boolean
67 68 69 |
# File 'lib/feed_updater/vendor/daemons/pidfile.rb', line 67 def exists? File.exists? filename end |
#filename ⇒ Object
63 64 65 |
# File 'lib/feed_updater/vendor/daemons/pidfile.rb', line 63 def filename File.join(@dir, "#{@progname}#{ @number or '' }.pid") end |
#pid ⇒ Object
91 92 93 94 95 |
# File 'lib/feed_updater/vendor/daemons/pidfile.rb', line 91 def pid File.open(filename) {|f| return f.gets.to_i } end |
#pid=(p) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/feed_updater/vendor/daemons/pidfile.rb', line 71 def pid=(p) if multiple while File.exists?(filename) and @number < 1024 @number += 1 end if @number == 1024 raise RuntimeException('cannot run more than 1024 instances of the application') end end File.open(filename, 'w') {|f| f.puts p #Process.pid } end |