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



51
52
# File 'lib/daemons/pid.rb', line 51

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.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/daemons/pid.rb', line 34

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, additional = nil) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/daemons/pid.rb', line 6

def Pid.running?(pid, additional = nil)
  match_pid = Regexp.new("^\\s*#{pid}\\s")
  got_match = false

  ps_all = IO.popen("ps ax") # the correct syntax is without a dash (-) !
  ps_all.each { |psline|
    next unless psline =~ match_pid
    got_match = true
    got_match = false if additional and psline !~ /#{additional}/
    break
  }
  ps_all.close

  # an alternative would be to use the code below, but I don't know whether this is portable
  # `ps axo pid=`.split.include? pid.to_s
   
  return got_match
end

Instance Method Details

#cleanupObject

Cleanup method



64
65
# File 'lib/daemons/pid.rb', line 64

def cleanup
end

#exists?Boolean

Exists? method

Returns:

  • (Boolean)


68
69
70
# File 'lib/daemons/pid.rb', line 68

def exists?
  true
end

#pidObject

Get method



56
57
# File 'lib/daemons/pid.rb', line 56

def pid
end

#pid=(p) ⇒ Object

Set method



60
61
# File 'lib/daemons/pid.rb', line 60

def pid=(p)
end