Class: DaemonKit::PidFile
Overview
Simple pidfile handling for daemon processes
Instance Method Summary collapse
- #cleanup ⇒ Object (also: #zap)
- #ensure_stopped! ⇒ Object
- #exists? ⇒ Boolean
-
#initialize(path) ⇒ PidFile
constructor
A new instance of PidFile.
-
#pid ⇒ Object
Return the pid contained in the pidfile, or nil.
-
#running? ⇒ Boolean
Returns true if the process is running.
- #write! ⇒ Object
Constructor Details
#initialize(path) ⇒ PidFile
Returns a new instance of PidFile.
6 7 8 |
# File 'lib/daemon_kit/pid_file.rb', line 6 def initialize( path ) @path = path.to_absolute_path end |
Instance Method Details
#cleanup ⇒ Object Also known as: zap
50 51 52 |
# File 'lib/daemon_kit/pid_file.rb', line 50 def cleanup File.delete( @path ) rescue Errno::ENOENT end |
#ensure_stopped! ⇒ Object
43 44 45 46 47 48 |
# File 'lib/daemon_kit/pid_file.rb', line 43 def ensure_stopped! if self.running? puts "Process already running with id #{self.pid}" exit 1 end end |
#exists? ⇒ Boolean
10 11 12 |
# File 'lib/daemon_kit/pid_file.rb', line 10 def exists? File.exists?( @path ) end |
#pid ⇒ Object
Return the pid contained in the pidfile, or nil
35 36 37 38 39 40 41 |
# File 'lib/daemon_kit/pid_file.rb', line 35 def pid return nil unless self.exists? File.open( @path ) { |f| return f.gets.to_i } end |
#running? ⇒ Boolean
Returns true if the process is running
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/daemon_kit/pid_file.rb', line 15 def running? return false unless self.exists? # Check if process is in existence # The simplest way to do this is to send signal '0' # (which is a single system call) that doesn't actually # send a signal begin Process.kill(0, self.pid) return true rescue Errno::ESRCH return false rescue ::Exception # for example on EPERM (process exists but does not belong to us) return true #rescue Errno::EPERM # return false end end |