Class: Daemons::Pid

Inherits:
Object
  • Object
show all
Defined in:
lib/daemons/pid.rb

Direct Known Subclasses

PidFile, PidMem

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePid

Initialization method



85
86
# File 'lib/daemons/pid.rb', line 85

def initialize
end

Class Method Details

.dir(dir_mode, dir, script) ⇒ Object

Returns the directory that should be used to write the pid file to depending on the given mode.

Some modes may require an additionaly hint, others may determine the directory automatically.

If no valid directory is found, returns nil.



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

def Pid.dir(dir_mode, dir, script)
  # nil script parameter is allowed as long as dir_mode is not :script
  return nil if dir_mode == :script && script.nil?                         
  
  case dir_mode
    when :normal
      return File.expand_path(dir)
    when :script
      return File.expand_path(File.join(File.dirname(script),dir))
    when :system  
      return '/var/run'
    else
      raise Error.new("pid file mode '#{dir_mode}' not implemented")
  end
end

.running?(pid) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/daemons/pid.rb', line 8

def Pid.running?(pid)
  # We have seen an odd problem where the pid file exists but is empty
  # In this case we want to not send a kill but we do want to zap the file
  # Thus while technically pid==0 => this process => running=true, treat it as running=false
  return false if ! pid || pid == 0
  
  # 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, pid)
    return true
  rescue TimeoutError
    raise
  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

Instance Method Details

#cleanupObject

Cleanup method



103
104
# File 'lib/daemons/pid.rb', line 103

def cleanup
end

#exist?Boolean

Exist? method

Returns:

  • (Boolean)


107
108
109
# File 'lib/daemons/pid.rb', line 107

def exist?
  true
end

#pidObject

Get method



90
91
# File 'lib/daemons/pid.rb', line 90

def pid
end

#pid=(p) ⇒ Object

Set method



94
95
# File 'lib/daemons/pid.rb', line 94

def pid=(p)
end

#running?Boolean

Check whether the process is running

Returns:

  • (Boolean)


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

def running?
  return Pid.running?(pid())
end