Class: ProcessMonitor

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

Class Method Summary collapse

Class Method Details

.get_pid(process_type, process_pattern = "") ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/process_monitor.rb', line 3

def self.get_pid(process_type,process_pattern="")
  # Parse the output to fetch the process id.
  process_id_reg_exp = %r{^(.*)\s*}

  process_information_lines = `pgrep #{process_type}|xargs ps`.split("\n")
  pids = []

  process_information_lines.each_with_index do |process_line,index|
    next if index == 0
    # The name of the process will be verity-spider
    # This is to make sure we don't kill any other ruby process other than spider.
    if process_line =~ /#{process_pattern}/
      process_id_reg_exp.match(process_line)
      pid =  $1.gsub(/\s*/, "").to_i
      pids << pid unless $$ == pid
    end
  end
  if pids.length > 1
    pids
  else
    pids.first
  end
end

.get_process_status(pid = nil) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/process_monitor.rb', line 28

def self.get_process_status(pid=nil)
  unless pid.nil?
    `cat /proc/#{pid}/status|grep State`
  else
    p "No pid given!"
  end
end

.process_is_up?(pid) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
46
# File 'lib/process_monitor.rb', line 37

def self.process_is_up?(pid)
  unless pid.nil?
    # The folder /proc/<pid>/ contains information about the process
    # The file /proc/<pid>/status gives information about the process as to if it is sleeping and so on.
    # If the folder exists it would mean the process exists.
    (pid.nil? || !File.exists?("/proc/#{pid}"))? false : true
  else
     p "No pid given!"
  end
end