Sha256: 44b22a20647a284807aaa92e27fa96e54702b17cb7d535c474025dd4b1757456

Contents?: true

Size: 1.28 KB

Versions: 49

Compression:

Stored size: 1.28 KB

Contents

module DaemonKit

  # Simple pidfile handling for daemon processes
  class PidFile

    def initialize( path )
      @path = path.to_absolute_path
    end

    def exists?
      File.exists?( @path )
    end

    # Returns true if the process is running
    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

    # Return the pid contained in the pidfile, or nil
    def pid
      return nil unless self.exists?

      File.open( @path ) { |f|
        return f.gets.to_i
      }
    end

    def ensure_stopped!
      if self.running?
        puts "Process already running with id #{self.pid}"
        exit 1
      end
    end

    def cleanup
      File.delete( @path ) rescue Errno::ENOENT
    end
    alias zap cleanup

    def write!
      File.open( @path, 'w' ) { |f|
        f.puts Process.pid
      }
    end
  end
end

Version data entries

49 entries across 49 versions & 7 rubygems

Version Path
daemon-kit-0.3.3 lib/daemon_kit/pid_file.rb
daemon-kit-0.3.2 lib/daemon_kit/pid_file.rb
kennethkalmer-daemon-kit-0.1.7.10 lib/daemon_kit/pid_file.rb
kennethkalmer-daemon-kit-0.1.7.3 lib/daemon_kit/pid_file.rb
kennethkalmer-daemon-kit-0.1.7.4 lib/daemon_kit/pid_file.rb
kennethkalmer-daemon-kit-0.1.7.5 lib/daemon_kit/pid_file.rb
kennethkalmer-daemon-kit-0.1.7.7 lib/daemon_kit/pid_file.rb
kennethkalmer-daemon-kit-0.1.7.8 lib/daemon_kit/pid_file.rb
kennethkalmer-daemon-kit-0.1.7.9 lib/daemon_kit/pid_file.rb
queso-daemon-kit-0.1.7.10 lib/daemon_kit/pid_file.rb
skaar-daemon-kit-0.1.7.10 lib/daemon_kit/pid_file.rb
daemon-kit-0.3.1 lib/daemon_kit/pid_file.rb
daemon-kit-0.3.0 lib/daemon_kit/pid_file.rb
daemon-kit-0.3.0.rc2 lib/daemon_kit/pid_file.rb
daemon-kit-0.3.0.rc1 lib/daemon_kit/pid_file.rb
daemon-kit-0.2.3 lib/daemon_kit/pid_file.rb
daemon-kit-0.2.1 lib/daemon_kit/pid_file.rb
daemon-kit-0.2.0 lib/daemon_kit/pid_file.rb
daemon-kit-0.1.8.2 lib/daemon_kit/pid_file.rb
amqp-daemon-kit-1.0.1 lib/daemon_kit/pid_file.rb