process_monitor 0.0.0 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ .idea/*
2
+
3
+ pkg/*
data/README CHANGED
@@ -3,3 +3,29 @@ Process Monitor
3
3
  A simple utility to get the process id and it status.
4
4
 
5
5
  The input required are the process type i.e. ruby,java or so on and a pattern to identify the process.
6
+
7
+ The pattern here could be any part of the name of the process to allow you to get an exact match of the process.
8
+
9
+ For example if you want to get the pid of all Ruby processes do this:
10
+
11
+ ProcessMonitor.get_pid("ruby")
12
+
13
+ will return an array of pids in case of many Ruby processes and return a single pid in case of a single Ruby process.
14
+
15
+ If you want to get the status of a process
16
+
17
+ ProcessMonitor.get_process_status(pid)
18
+
19
+ will return the state of the process i.e. running/sleeping etc.
20
+
21
+ if you want to check if a process is up
22
+
23
+ ProcessMonitor.process_is_up?(pid)
24
+
25
+ will return "true" or "false"
26
+
27
+
28
+
29
+
30
+
31
+
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.0
1
+ 0.1.0
@@ -1,38 +1,52 @@
1
1
  class ProcessMonitor
2
2
 
3
- def get_pid(process_type,process_pattern)
3
+ def self.get_pid(process_type,process_pattern="")
4
4
  # Parse the output to fetch the process id.
5
5
  process_id_reg_exp = %r{^(.*)\s*}
6
6
 
7
7
  process_information_lines = `pgrep #{process_type}|xargs ps`.split("\n")
8
- pid = nil
8
+ pids = []
9
9
 
10
- process_information_lines.each do |process_line|
10
+ process_information_lines.each_with_index do |process_line,index|
11
+ next if index == 0
11
12
  # The name of the process will be verity-spider
12
13
  # This is to make sure we don't kill any other ruby process other than spider.
13
14
  if process_line =~ /#{process_pattern}/
14
15
  process_id_reg_exp.match(process_line)
15
- pid = $1.gsub(/\s*/, "").to_i
16
+ pid = $1.gsub(/\s*/, "").to_i
17
+ pids << pid unless $$ == pid
16
18
  end
17
19
  end
18
- pid
20
+ if pids.length > 1
21
+ pids
22
+ else
23
+ pids.first
24
+ end
19
25
  end
20
26
 
21
27
 
22
- def self.get_process_status(process_type,process_pattern)
23
- pid = self.get_pid(process_type,process_pattern)
24
- `cat /proc/#{pid}/status|grep State`
28
+ def self.get_process_status(pid=nil)
29
+ unless pid.nil?
30
+ `cat /proc/#{pid}/status|grep State`
31
+ else
32
+ p "No pid given!"
33
+ end
25
34
  end
26
35
 
27
36
 
28
- def self.process_is_up?(process_type,process_pattern)
29
- pid = self.get_pid(process_type,process_pattern)
30
- # The folder /proc/<pid>/ contains information about the process
31
- # The file /proc/<pid>/status gives information about the process as to if it is sleeping and so on.
32
- # If the folder exists it would mean the process exists.
33
- (pid.nil? || !File.exists?("/proc/#{pid}"))? false : true
37
+ def self.process_is_up?(pid)
38
+ unless pid.nil?
39
+ # The folder /proc/<pid>/ contains information about the process
40
+ # The file /proc/<pid>/status gives information about the process as to if it is sleeping and so on.
41
+ # If the folder exists it would mean the process exists.
42
+ (pid.nil? || !File.exists?("/proc/#{pid}"))? false : true
43
+ else
44
+ p "No pid given!"
45
+ end
34
46
  end
35
47
 
36
48
  end
37
49
 
38
50
 
51
+
52
+
@@ -0,0 +1,41 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{process_monitor}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Sriram Varahan"]
12
+ s.date = %q{2010-06-24}
13
+ s.description = %q{This gem gets the status(running/sleeping/defunct) based on a pattern you provide to it. For example if you have a service which is running and its a ruby service you would need to pass the type(ruby) and a pattern which would identify the process.}
14
+ s.email = %q{sriram.varahan@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "README",
21
+ "Rakefile",
22
+ "VERSION",
23
+ "lib/process_monitor.rb",
24
+ "process_monitor.gemspec"
25
+ ]
26
+ s.rdoc_options = ["--charset=UTF-8"]
27
+ s.require_paths = ["lib"]
28
+ s.rubygems_version = %q{1.3.7}
29
+ s.summary = %q{A gem to monitor processes}
30
+
31
+ if s.respond_to? :specification_version then
32
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
33
+ s.specification_version = 3
34
+
35
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
36
+ else
37
+ end
38
+ else
39
+ end
40
+ end
41
+
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: process_monitor
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 0
10
- version: 0.0.0
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sriram Varahan
@@ -28,10 +28,12 @@ extensions: []
28
28
  extra_rdoc_files:
29
29
  - README
30
30
  files:
31
+ - .gitignore
31
32
  - README
32
33
  - Rakefile
33
34
  - VERSION
34
35
  - lib/process_monitor.rb
36
+ - process_monitor.gemspec
35
37
  has_rdoc: true
36
38
  homepage:
37
39
  licenses: []