get_running_processes 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -27,16 +27,33 @@ Or install it yourself as:
27
27
 
28
28
  require 'get_running_processes'
29
29
 
30
- processes = GetRunningProcesses.collect_processes
31
-
32
30
  The format of the output of 'ps -ef' is as follows:
33
31
 
34
32
  UID PID PPID C STIME TTY TIME CMD
35
33
  root 1 0 0 Sep12 ? 00:00:00 /sbin/init
36
34
 
37
- The return value of GetRunningProcesses.collect_processes is an Array of Arrays:
35
+ The most simple output:
36
+
37
+ processes = GetRunningProcesses.collect_processes
38
+
39
+ => [["root", "1", "0", "0", "Sep12", "?", "00:00:00", "/sbin/init"]]
40
+
41
+ Other output:
42
+
43
+ processes = GetRunningProcesses.processes
44
+
45
+ processes.all
46
+
47
+ => [{"UID"=>"root", "PID"=>"1", "PPID"=>"0", "C"=>"0", "STIME"=>"09:01", "TTY"=>"?", "TIME"=>"00:00:03", "CMD"=>"/sbin/init"}]
48
+
49
+ processes.commands
50
+
51
+ => ["/sbin/init"]
52
+
53
+ processes.commands_with_pids
54
+
55
+ => [{1 => "/sbin/init"}]
38
56
 
39
- [["root", "1", "0", "0", "Sep12", "?", "00:00:00", "/sbin/init"]]
40
57
 
41
58
  ## Contributing
42
59
 
@@ -1,3 +1,3 @@
1
1
  module GetRunningProcesses
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -1,8 +1,12 @@
1
1
  require 'get_running_processes/version'
2
- require 'collect_processes'
2
+ require 'processes'
3
3
 
4
4
  module GetRunningProcesses
5
5
  def self.collect_processes
6
- CollectProcesses.read_processes
6
+ Processes.get_process_array
7
+ end
8
+
9
+ def self.processes
10
+ Processes.new
7
11
  end
8
12
  end
data/lib/processes.rb ADDED
@@ -0,0 +1,96 @@
1
+ module GetRunningProcesses
2
+ class Processes
3
+ attr_reader :commands,
4
+ :commands_with_pids,
5
+ :all
6
+
7
+ def initialize
8
+ @all = Processes.get_all
9
+ @commands = Processes.get_commands
10
+ @commands_with_pids = Processes.get_commands_with_pids
11
+ end
12
+
13
+ def self.get_commands
14
+ processes = Processes.get_process_array
15
+
16
+ commands = []
17
+
18
+ processes.each do |process|
19
+ commands << process[7]
20
+ end
21
+
22
+ return commands
23
+ end
24
+
25
+ def self.get_commands_with_pids
26
+ processes = Processes.get_process_array
27
+
28
+ commands_with_pids = []
29
+
30
+ processes.each do |process|
31
+ command = Hash.new
32
+
33
+ command[process[1].to_i] = process[7]
34
+
35
+ commands_with_pids << command
36
+ end
37
+
38
+ return commands_with_pids
39
+ end
40
+
41
+ def self.get_all
42
+ processes = Processes.get_process_array
43
+
44
+ all = []
45
+
46
+ processes.each do |process|
47
+ command = Hash.new
48
+
49
+ command["UID"] = process[0]
50
+ command["PID"] = process[1]
51
+ command["PPID"] = process[2]
52
+ command["C"] = process[3]
53
+ command["STIME"] = process[4]
54
+ command["TTY"] = process[5]
55
+ command["TIME"] = process[6]
56
+ command["CMD"] = process[7]
57
+
58
+ all << command
59
+ end
60
+
61
+ return all
62
+ end
63
+
64
+ def self.get_process_array
65
+ output = `ps -ef`
66
+
67
+ # Container for returned processes
68
+ processes = []
69
+
70
+ # Container for processing the lines
71
+ lines = []
72
+
73
+ # Grab the lines from ps and collect into Arrays
74
+ output.lines.each do |line|
75
+ process = line.split(' ')
76
+ lines << process
77
+ end
78
+
79
+ # Process the lines from ps and concat the command names
80
+ lines.each do |line|
81
+ # Skip the header row
82
+ # TODO: Use this header row to extract PID, CMD, etc.
83
+ if line[0] == "UID"
84
+ else
85
+ # Concatenate the command after splitting on spaces
86
+ result = line.slice!(7, 30).join(' ').strip
87
+ line << result
88
+ processes << line
89
+ end
90
+ end
91
+
92
+ # Returns the Array
93
+ return processes
94
+ end
95
+ end
96
+ end
@@ -9,5 +9,26 @@ describe GetRunningProcesses do
9
9
  expect(processes[0][7]).to eq('/sbin/init')
10
10
  end
11
11
  end
12
+
13
+ describe "#commands" do
14
+ it "returns an array of the running commands" do
15
+ processes = GetRunningProcesses.processes
16
+ expect(processes.commands).to include("ps -ef")
17
+ end
18
+ end
19
+
20
+ describe "#commands_with_pids" do
21
+ it "returns an array of hashes with the pid as the key and the command as a value" do
22
+ processes = GetRunningProcesses.processes
23
+ expect(processes.commands_with_pids).to include({1 => "/sbin/init"})
24
+ end
25
+ end
26
+
27
+ describe "#all" do
28
+ it "returns an array of hashes with all information from the ps output" do
29
+ processes = GetRunningProcesses.processes
30
+ expect(processes.all).to include({"UID"=>"root", "PID"=>"1", "PPID"=>"0", "C"=>"0", "STIME"=>"09:01", "TTY"=>"?", "TIME"=>"00:00:03", "CMD"=>"/sbin/init"})
31
+ end
32
+ end
12
33
  end
13
34
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: get_running_processes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-16 00:00:00.000000000 Z
12
+ date: 2012-09-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: !ruby/object:Gem::Requirement
16
+ requirement: &17902540 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,15 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '0'
24
+ version_requirements: *17902540
30
25
  - !ruby/object:Gem::Dependency
31
26
  name: rspec
32
- requirement: !ruby/object:Gem::Requirement
27
+ requirement: &17901960 !ruby/object:Gem::Requirement
33
28
  none: false
34
29
  requirements:
35
30
  - - ! '>='
@@ -37,12 +32,7 @@ dependencies:
37
32
  version: '0'
38
33
  type: :development
39
34
  prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
45
- version: '0'
35
+ version_requirements: *17901960
46
36
  description: Get running processes on a linux system through the output of 'ps -ef'
47
37
  email:
48
38
  - casey.ellett@gmail.com
@@ -58,9 +48,9 @@ files:
58
48
  - README.md
59
49
  - Rakefile
60
50
  - get_running_processes.gemspec
61
- - lib/collect_processes.rb
62
51
  - lib/get_running_processes.rb
63
52
  - lib/get_running_processes/version.rb
53
+ - lib/processes.rb
64
54
  - spec/get_running_processes_spec.rb
65
55
  - spec/spec_helper.rb
66
56
  homepage: https://github.com/odin/get_running_processes
@@ -75,21 +65,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
75
65
  - - ! '>='
76
66
  - !ruby/object:Gem::Version
77
67
  version: '0'
78
- segments:
79
- - 0
80
- hash: 1180402951549833394
81
68
  required_rubygems_version: !ruby/object:Gem::Requirement
82
69
  none: false
83
70
  requirements:
84
71
  - - ! '>='
85
72
  - !ruby/object:Gem::Version
86
73
  version: '0'
87
- segments:
88
- - 0
89
- hash: 1180402951549833394
90
74
  requirements: []
91
75
  rubyforge_project:
92
- rubygems_version: 1.8.24
76
+ rubygems_version: 1.8.17
93
77
  signing_key:
94
78
  specification_version: 3
95
79
  summary: Get running processes from ps
@@ -1,27 +0,0 @@
1
- module GetRunningProcesses
2
- class CollectProcesses
3
- def self.read_processes
4
- output = `ps -ef`
5
-
6
- processes = []
7
-
8
- lines = []
9
-
10
- output.lines.each do |line|
11
- process = line.split(' ')
12
- lines << process
13
- end
14
-
15
- lines.each do |line|
16
- if line[0] == "UID"
17
- else
18
- result = line.slice!(7, 30).join(' ').strip
19
- line << result
20
- processes << line
21
- end
22
- end
23
-
24
- return processes
25
- end
26
- end
27
- end