ps-ruby 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b3ff7a462e7aaeba2c9ee15d511328806f0216b9
4
- data.tar.gz: 4490d6b8aaaa391dde6c75b1222f76d7f3249e83
3
+ metadata.gz: c0ca66f32f6eca7fd6e96a2b7fb033f7bda48a19
4
+ data.tar.gz: f82c63cfe8d729ba672ea791fa52878e64a0bbba
5
5
  SHA512:
6
- metadata.gz: 71f7b329057d976c46e87db7c33159f11a6eabf684a8face4902ba8289466467b21ef2d84a10a289949edee80a2dcad0c7c5226ac578affe2cce42c3d377dba7
7
- data.tar.gz: bb5bdeed8cf3b42b21983aa0f3fc07b3db3b4385d6f95c9eb2e570124cadaa97f62f96e810de3806bfb6dfc0fd0530d5994615174f0fe21ec4bb913f0ba65462
6
+ metadata.gz: 1113ff303d2d5d39d606defdbd348a77bd0bd0403909699951358d1ac631b4042e5d216eb29e0339ec0bf631ee3d10433b01fdeb2580565f62011ca2621623e7
7
+ data.tar.gz: 875aef0cabdb82957b6004e971f0708235850cc55428d9703ce6df322ddee8c0f36781e3087fa3b8f0ec8d31dea7b8c33afa41aa9529ffcd45c66a2e03afe8c2
@@ -1,13 +1,25 @@
1
1
 
2
- module PS
3
- module_function
4
2
 
5
- def simple_display(name = "", limit_process_name_len=30)
6
- result = find_process(name)
3
+ class PsProcess < Hash
4
+ def kill!
5
+ `kill #{self["PID"]}`
6
+ end
7
+
8
+ def alive?
9
+ PS.get_all_processes.pick_by_attr("PID").include?(self["PID"])
10
+ end
11
+ end
12
+
13
+ class PsProcessList < Array
14
+ def kill!
15
+ self.each(&:kill!)
16
+ end
17
+
18
+ def simple_display(limit_process_name_len=30)
7
19
  picks = ["PID", "%CPU", "%MEM", "COMMAND"]
8
- if result.size > 0
20
+ if self.size > 0
9
21
  puts picks.join("\t")+"\n"
10
- result.each{|x|
22
+ self.each{|x|
11
23
  puts picks.reduce(""){|s, k|
12
24
  s += if k == "COMMAND" and x[k].size > limit_process_name_len then
13
25
  "#{x[k][0..limit_process_name_len]}...\t"
@@ -23,31 +35,52 @@ module PS
23
35
  end
24
36
 
25
37
  def find_process(name)
26
- regex = if name.class != Regexp then Regexp.new(".*#{name}.*", Regexp::IGNORECASE) else name end
27
- aux.select{|x| x["COMMAND"] =~ regex }
38
+ find_process_by("COMMAND", name)
39
+ end
40
+
41
+ def find_process_by(attr_name, value)
42
+ regex = if value.class != Regexp then Regexp.new(".*#{value}.*", Regexp::IGNORECASE) else value end
43
+ PsProcessList.new(self.select{|x| x[attr_name] =~ regex })
44
+ end
45
+
46
+ def pick_by_attr(attr_name)
47
+ self.map{|x| x[attr_name] } if PS.attrs.include?(attr_name)
48
+ end
49
+
50
+ end
51
+
52
+
53
+
54
+
55
+ module PS
56
+ module_function
57
+
58
+ def simple_display(name = "", limit_process_name_len=30)
59
+ find_process(name).simple_display(limit_process_name_len)
60
+ end
61
+
62
+ def find_process(name)
63
+ find_process_by("COMMAND", name)
28
64
  end
29
65
 
30
- def processes
31
- pick_attr("COMMAND").uniq.sort
66
+ def find_process_by(attr_name, value)
67
+ get_all_processes.find_process_by(attr_name, value)
32
68
  end
33
69
 
34
70
  def attrs
71
+ # ["USER", "PID", "%CPU", "%MEM", "VSZ", "RSS", "TT", "STAT", "STARTED", "TIME", "COMMAND"]
35
72
  return @ps_attrs if @ps_attrs
36
73
  @ps_attrs = raw_aux.split("\n").map{|x| x.split(/\s+/) }.shift
37
74
  end
38
75
 
39
- def pick_attr(attr_name)
40
- return aux.map{|x| x[attr_name]} if attrs.include?(attr_name)
41
- end
42
-
43
- def aux
76
+ def get_all_processes
44
77
  table = raw_aux.split("\n").map{|x| x.split(/\s+/) }
45
78
  attrs = table.shift
46
- table = table.reduce([]) {|s, x|
79
+ table = table.reduce(PsProcessList.new) {|s, x|
47
80
  if x.size > attrs.size
48
81
  x = x[0..(attrs.size-2)] + [x[(attrs.size-1)..-1].join(" ")]
49
82
  end
50
- s << Hash[attrs.zip(x)]
83
+ s << PsProcess[attrs.zip(x)]
51
84
  }
52
85
  table
53
86
  end
@@ -2,6 +2,6 @@ module PS
2
2
  module_function
3
3
 
4
4
  def VERSION
5
- "0.0.1"
5
+ "0.0.2"
6
6
  end
7
7
  end
Binary file
data/readme.md CHANGED
@@ -1,14 +1,20 @@
1
1
 
2
2
  # PS-Ruby
3
3
 
4
- PS-Ruby is a simple ps wrapper with ruby. You can see (example.rb)[example.rb] to learn more...
4
+ PS-Ruby is a simple ps wrapper with ruby. You can see [example.rb](example/example.rb) to learn more...
5
5
 
6
- # Usage
6
+ # installation
7
7
 
8
+ ```
9
+ gem install ps-ruby
10
+ ```
8
11
 
9
- Display all processes
12
+ # Usage
10
13
 
11
- ```
14
+ ## Display all processes
15
+
16
+ ```ruby
17
+ > require 'ps-ruby'
12
18
  > PS.simple_display
13
19
  ```
14
20
 
@@ -21,18 +27,24 @@ Display all processes
21
27
  ...
22
28
  ```
23
29
 
24
- Display special process by name
30
+ ## Display special process by name
25
31
 
26
- ```
32
+ ```ruby
27
33
  > PS.simple_display("zsh")
28
34
  ```
29
35
 
30
36
  or
31
37
 
32
- ```
38
+ ```ruby
33
39
  > PS.simple_display(/.*zsh.*/i)
34
40
  ```
35
41
 
42
+ or
43
+
44
+ ```ruby
45
+ PS.find_process("zsh").simple_display
46
+ ```
47
+
36
48
  ```
37
49
  PID %CPU %MEM COMMAND
38
50
  59395 0.4 0.0 -zsh
@@ -40,21 +52,98 @@ or
40
52
  83005 0.0 0.0 -zsh
41
53
  ```
42
54
 
43
- Get special process by name
55
+ ## Get special process by name
44
56
 
45
57
 
46
- ```
58
+ ```ruby
47
59
  > PS.find_process("zsh")
48
60
  ```
49
61
 
62
+ ```ruby
63
+ [{
64
+ "USER" => "HondaDai",
65
+ "PID" => "59395",
66
+ "%CPU" => "0.4",
67
+ "%MEM" => "0.0",
68
+ "VSZ" => "2462760",
69
+ "RSS" => "3308",
70
+ "TT" => "s002",
71
+ "STAT" => "S",
72
+ "STARTED" => "2:21PM",
73
+ "TIME" => "0:00.75",
74
+ "COMMAND" => "-zsh"
75
+ }, {
76
+ "USER" => "HondaDai",
77
+ "PID" => "83005",
78
+ "%CPU" => "0.0",
79
+ "%MEM" => "0.0",
80
+ "VSZ" => "2462760",
81
+ "RSS" => "776",
82
+ "TT" => "s001",
83
+ "STAT" => "S+",
84
+ "STARTED" => "10:10PM",
85
+ "TIME" => "0:01.07",
86
+ "COMMAND" => "-zsh"
87
+ }, {
88
+ "USER" => "HondaDai",
89
+ "PID" => "67468",
90
+ "%CPU" => "0.0",
91
+ "%MEM" => "0.0",
92
+ "VSZ" => "2462760",
93
+ "RSS" => "196",
94
+ "TT" => "s000",
95
+ "STAT" => "S",
96
+ "STARTED" => "Fri03PM",
97
+ "TIME" => "0:00.78",
98
+ "COMMAND" => "-zsh"
99
+ }]
100
+ ```
101
+
102
+ ## Chaining
103
+
104
+ ```ruby
105
+ > PS.find_process("zsh").find_process_by("PID", "67468")
106
+ ```
107
+
50
108
  ```
51
- [{"USER"=>"HondaDai", "PID"=>"59395", "%CPU"=>"0.4", "%MEM"=>"0.0", "VSZ"=>"2462760", "RSS"=>"3308", "TT"=>"s002", "STAT"=>"S", "STARTED"=>"2:21PM", "TIME"=>"0:00.75", "COMMAND"=>"-zsh"}, {"USER"=>"HondaDai", "PID"=>"83005", "%CPU"=>"0.0", "%MEM"=>"0.0", "VSZ"=>"2462760", "RSS"=>"776", "TT"=>"s001", "STAT"=>"S+", "STARTED"=>"10:10PM", "TIME"=>"0:01.07", "COMMAND"=>"-zsh"}, {"USER"=>"HondaDai", "PID"=>"67468", "%CPU"=>"0.0", "%MEM"=>"0.0", "VSZ"=>"2462760", "RSS"=>"196", "TT"=>"s000", "STAT"=>"S", "STARTED"=>"Fri03PM", "TIME"=>"0:00.78", "COMMAND"=>"-zsh"}]
109
+ [{
110
+ "USER" => "HondaDai",
111
+ "PID" => "67468",
112
+ "%CPU" => "0.0",
113
+ "%MEM" => "0.0",
114
+ "VSZ" => "2462760",
115
+ "RSS" => "196",
116
+ "TT" => "s000",
117
+ "STAT" => "S",
118
+ "STARTED" => "Fri03PM",
119
+ "TIME" => "0:00.78",
120
+ "COMMAND" => "-zsh"
121
+ }]
52
122
  ```
53
123
 
54
- Get all processes
124
+ ## Kill process
55
125
 
56
126
  ```ruby
57
- > PS.processes
127
+ > PS.find_process("chrome").kill! # kill all processes named '.*chrome.*'
58
128
  ```
59
129
 
60
- omited
130
+ or
131
+
132
+ ```ruby
133
+ > chrome = PS.find_process("chrome")[0]
134
+ > chrome.kill!
135
+ ```
136
+
137
+
138
+ ## Check process alive
139
+
140
+ ```ruby
141
+ > chrome = PS.find_process("chrome")[0]
142
+ > chrome.alive?
143
+ ```
144
+
145
+ ## Get all processes
146
+
147
+ ```ruby
148
+ > PS.get_all_processes
149
+ ```
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ps-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - HondaDai
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-02 00:00:00.000000000 Z
11
+ date: 2015-04-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -26,7 +26,7 @@ files:
26
26
  - lib/ps-ruby.rb
27
27
  - lib/ps-ruby/ps-ruby.rb
28
28
  - lib/ps-ruby/version.rb
29
- - pkg/ps-ruby-0.0.1.gem
29
+ - ps-ruby-0.0.1.gem
30
30
  - ps-ruby.gemspec
31
31
  - readme.md
32
32
  homepage: https://github.com/HondaDai/ps-ruby