get_running_processes 0.0.5 → 1.0.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/README.md +26 -18
- data/lib/get_running_processes.rb +3 -3
- data/lib/get_running_processes/processes.rb +97 -0
- data/lib/get_running_processes/version.rb +1 -1
- data/spec/get_running_processes_spec.rb +17 -8
- metadata +18 -8
- data/lib/processes.rb +0 -96
data/README.md
CHANGED
@@ -25,35 +25,43 @@ Or install it yourself as:
|
|
25
25
|
|
26
26
|
## Usage
|
27
27
|
|
28
|
-
require 'get_running_processes'
|
29
|
-
|
30
28
|
The format of the output of 'ps -ef' is as follows:
|
31
29
|
|
32
30
|
UID PID PPID C STIME TTY TIME CMD
|
33
31
|
root 1 0 0 Sep12 ? 00:00:00 /sbin/init
|
34
32
|
|
35
|
-
The most simple output:
|
33
|
+
The most simple output:
|
34
|
+
|
35
|
+
``` ruby
|
36
|
+
require 'get_running_processes'
|
36
37
|
|
37
|
-
|
38
|
+
processes = GetRunningProcesses.collect_processes
|
38
39
|
|
39
|
-
|
40
|
-
|
40
|
+
#=> [["root", "1", "0", "0", "Sep12", "?", "00:00:00", "/sbin/init"]]
|
41
|
+
```
|
41
42
|
Other output:
|
42
43
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
44
|
+
``` ruby
|
45
|
+
require 'get_running_processes'
|
46
|
+
|
47
|
+
processes = GetRunningProcesses.processes
|
48
|
+
|
49
|
+
processes.all
|
50
|
+
|
51
|
+
#=> [{"UID"=>"root", "PID"=>1, "PPID"=>0, "C"=>"0", "STIME"=>"09:01", "TTY"=>"?", "TIME"=>"00:00:03", "CMD"=>"/sbin/init"}]
|
52
|
+
|
53
|
+
processes.commands
|
54
|
+
|
55
|
+
#=> ["/sbin/init"]
|
56
|
+
|
57
|
+
processes.commands_with_pids
|
58
|
+
|
59
|
+
#=> [{"/sbin/init" => 1}]
|
48
60
|
|
49
|
-
|
50
|
-
|
51
|
-
=> ["/sbin/init"]
|
52
|
-
|
53
|
-
processes.commands_with_pids
|
54
|
-
|
55
|
-
=> [{1 => "/sbin/init"}]
|
61
|
+
processes.pids_with_commands
|
56
62
|
|
63
|
+
#=> [{1 => "/sbin/init"}]
|
64
|
+
```
|
57
65
|
|
58
66
|
## Contributing
|
59
67
|
|
@@ -1,11 +1,11 @@
|
|
1
1
|
require 'get_running_processes/version'
|
2
|
-
require 'processes'
|
2
|
+
require 'get_running_processes/processes'
|
3
3
|
|
4
4
|
module GetRunningProcesses
|
5
5
|
def self.collect_processes
|
6
|
-
Processes.get_process_array
|
6
|
+
Processes.new.get_process_array
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
def self.processes
|
10
10
|
Processes.new
|
11
11
|
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
module GetRunningProcesses
|
2
|
+
class Processes
|
3
|
+
attr_reader :all,
|
4
|
+
:commands,
|
5
|
+
:commands_with_pids,
|
6
|
+
:pids_with_commands
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@all = self.get_all
|
10
|
+
@commands = self.get_commands
|
11
|
+
@commands_with_pids = self.get_commands_with_pids
|
12
|
+
end
|
13
|
+
|
14
|
+
def get_all
|
15
|
+
processes = self.get_process_array
|
16
|
+
all = []
|
17
|
+
|
18
|
+
processes.each do |process|
|
19
|
+
command = {}
|
20
|
+
|
21
|
+
command["UID"] = process[0]
|
22
|
+
command["PID"] = process[1].to_i
|
23
|
+
command["PPID"] = process[2].to_i
|
24
|
+
command["C"] = process[3]
|
25
|
+
command["STIME"] = process[4]
|
26
|
+
command["TTY"] = process[5]
|
27
|
+
command["TIME"] = process[6]
|
28
|
+
command["CMD"] = process[7]
|
29
|
+
|
30
|
+
all << command
|
31
|
+
end
|
32
|
+
|
33
|
+
return all
|
34
|
+
end
|
35
|
+
|
36
|
+
def get_commands
|
37
|
+
processes = self.get_process_array
|
38
|
+
commands = []
|
39
|
+
|
40
|
+
processes.each do |process|
|
41
|
+
commands << process[7]
|
42
|
+
end
|
43
|
+
|
44
|
+
return commands
|
45
|
+
end
|
46
|
+
|
47
|
+
def get_commands_with_pids
|
48
|
+
processes = self.get_process_array
|
49
|
+
commands_with_pids = []
|
50
|
+
|
51
|
+
processes.each do |process|
|
52
|
+
command = {}
|
53
|
+
command[process[7]] = process[1].to_i
|
54
|
+
|
55
|
+
commands_with_pids << command
|
56
|
+
end
|
57
|
+
|
58
|
+
return commands_with_pids
|
59
|
+
end
|
60
|
+
|
61
|
+
def pids_with_commands
|
62
|
+
processes = self.get_process_array
|
63
|
+
pids_with_commands = []
|
64
|
+
|
65
|
+
processes.each do |process|
|
66
|
+
command = {}
|
67
|
+
command[process[1].to_i] = process[7]
|
68
|
+
|
69
|
+
pids_with_commands << command
|
70
|
+
end
|
71
|
+
|
72
|
+
return pids_with_commands
|
73
|
+
end
|
74
|
+
|
75
|
+
def get_process_array
|
76
|
+
output = `ps -ef`
|
77
|
+
processes = []
|
78
|
+
lines = []
|
79
|
+
|
80
|
+
output.lines.each do |line|
|
81
|
+
process = line.split(' ')
|
82
|
+
lines << process
|
83
|
+
end
|
84
|
+
|
85
|
+
lines.each do |line|
|
86
|
+
if line[0] == "UID"
|
87
|
+
else
|
88
|
+
result = line.slice!(7, 30).join(' ').strip
|
89
|
+
line << result
|
90
|
+
processes << line
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
return processes
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -9,25 +9,34 @@ describe GetRunningProcesses do
|
|
9
9
|
expect(processes[0][7]).to eq('/sbin/init')
|
10
10
|
end
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
|
+
describe "#all" do
|
14
|
+
it "returns an array of hashes with all information from the ps output" do
|
15
|
+
processes = GetRunningProcesses.processes
|
16
|
+
expect(processes.all[0].keys).to eq(["UID", "PID", "PPID", "C", "STIME", "TTY", "TIME", "CMD"])
|
17
|
+
expect(processes.all[0]["UID"]).to eq("root")
|
18
|
+
expect(processes.all[0]["PID"]).to eq(1)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
13
22
|
describe "#commands" do
|
14
23
|
it "returns an array of the running commands" do
|
15
24
|
processes = GetRunningProcesses.processes
|
16
25
|
expect(processes.commands).to include("ps -ef")
|
17
26
|
end
|
18
27
|
end
|
19
|
-
|
28
|
+
|
20
29
|
describe "#commands_with_pids" do
|
21
|
-
it "returns an array of hashes with the
|
30
|
+
it "returns an array of hashes with the command as the key and the pid as a value" do
|
22
31
|
processes = GetRunningProcesses.processes
|
23
|
-
expect(processes.commands_with_pids).to include({
|
32
|
+
expect(processes.commands_with_pids).to include({ "/sbin/init" => 1 })
|
24
33
|
end
|
25
34
|
end
|
26
|
-
|
27
|
-
describe "#
|
28
|
-
it "returns an array of hashes with
|
35
|
+
|
36
|
+
describe "#pids_with_commands" do
|
37
|
+
it "returns an array of hashes with the pid as the key and the command as a value" do
|
29
38
|
processes = GetRunningProcesses.processes
|
30
|
-
expect(processes.
|
39
|
+
expect(processes.pids_with_commands).to include({ 1 => "/sbin/init" })
|
31
40
|
end
|
32
41
|
end
|
33
42
|
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
|
+
version: 1.0.0
|
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-
|
12
|
+
date: 2012-09-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,15 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
25
30
|
- !ruby/object:Gem::Dependency
|
26
31
|
name: rspec
|
27
|
-
requirement:
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
28
33
|
none: false
|
29
34
|
requirements:
|
30
35
|
- - ! '>='
|
@@ -32,7 +37,12 @@ dependencies:
|
|
32
37
|
version: '0'
|
33
38
|
type: :development
|
34
39
|
prerelease: false
|
35
|
-
version_requirements:
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
36
46
|
description: Get running processes on a linux system through the output of 'ps -ef'
|
37
47
|
email:
|
38
48
|
- casey.ellett@gmail.com
|
@@ -49,8 +59,8 @@ files:
|
|
49
59
|
- Rakefile
|
50
60
|
- get_running_processes.gemspec
|
51
61
|
- lib/get_running_processes.rb
|
62
|
+
- lib/get_running_processes/processes.rb
|
52
63
|
- lib/get_running_processes/version.rb
|
53
|
-
- lib/processes.rb
|
54
64
|
- spec/get_running_processes_spec.rb
|
55
65
|
- spec/spec_helper.rb
|
56
66
|
homepage: https://github.com/odin/get_running_processes
|
@@ -73,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
73
83
|
version: '0'
|
74
84
|
requirements: []
|
75
85
|
rubyforge_project:
|
76
|
-
rubygems_version: 1.8.
|
86
|
+
rubygems_version: 1.8.24
|
77
87
|
signing_key:
|
78
88
|
specification_version: 3
|
79
89
|
summary: Get running processes from ps
|
data/lib/processes.rb
DELETED
@@ -1,96 +0,0 @@
|
|
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
|