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.
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).
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 (:script option for :dir_mode)
in a directory given by :dir (:normal option for :dir_mode)
in the preconfigured directory /var/run (:system option for :dir_mode)
# File lib/daemons/pidfile.rb, line 55 55: def PidFile.existing(path) 56: new_instance = PidFile.allocate 57: 58: new_instance.instance_variable_set(:@path, path) 59: 60: def new_instance.filename 61: return @path 62: end 63: 64: return new_instance 65: end
# File lib/daemons/pidfile.rb, line 36 36: def PidFile.find_files(dir, progname, delete = false) 37: files = Dir[File.join(dir, "#{progname}*.pid")] 38: 39: files.delete_if {|f| not (File.file?(f) and File.readable?(f))} 40: if delete 41: files.delete_if do |f| 42: pid = File.open(f) {|h| h.read}.to_i 43: rsl = ! Pid.running?(pid) 44: if rsl 45: puts "pid-file for killed process #{pid} found (#{f}), deleting." 46: begin; File.unlink(f); rescue ::Exception; end 47: end 48: rsl 49: end 50: end 51: 52: return files 53: end
# File lib/daemons/pidfile.rb, line 67 67: def initialize(dir, progname, multiple = false) 68: @dir = File.expand_path(dir) 69: @progname = progname 70: @multiple = multiple 71: @number = nil 72: @number = 0 if multiple 73: 74: if multiple 75: while File.exist?(filename) and @number < 1024 76: @number += 1 77: end 78: 79: if @number == 1024 80: raise RuntimeException('cannot run more than 1024 instances of the application') 81: end 82: end 83: end
# File lib/daemons/pidfile.rb, line 100 100: def cleanup 101: File.delete(filename) if pid == Process.pid 102: end
# File lib/daemons/pidfile.rb, line 89 89: def exist? 90: File.exist? filename 91: end
# File lib/daemons/pidfile.rb, line 85 85: def filename 86: File.join(@dir, "#{@progname}#{ @number or '' }.pid") 87: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.