ps 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,43 @@
1
+ # PS Gem
2
+ A gem for interacting with the **p**rocess **s**tatus utility on unix systems.
3
+
4
+ ### Getting information on a specific process
5
+
6
+ [proc1,proc2] = PS.pid(1234,321)
7
+
8
+ or for shorthand
9
+
10
+ proc1 = PS(1234)
11
+ process_list = [proc1,proc2] = PS(1234,321)
12
+
13
+ ### Finding process based on name
14
+
15
+ process_list = PS(/Firefox.app/)
16
+
17
+ ### Finding process based on attached port
18
+
19
+ To return any processes attached to port 80.
20
+
21
+ process_list = PS(':80')
22
+
23
+ ## Process
24
+
25
+ Processes have methods for every value listed in `ps -L` with `%` aliased to `pct`. The method `mem` was added which gives the megabytes of memory used by the process. The method `kill!(sig="INT")` was also added to send a kill command to the process. And you can call `alive?` to poll if the process is still alive.
26
+
27
+ ## Example
28
+
29
+ If I wanted to kill firefox if it got more than 50% cpu or 600mb of memory.
30
+
31
+ fx = PS(/Firefox.app/).first
32
+ # only kill if taking up more than 50% cpu or 600mb of memory
33
+ exit unless fx.pcpu > 50 || fx.mem > 600
34
+ retries = 0
35
+ fx.kill!
36
+ while fx.alive?
37
+ retries += 1
38
+ if retries > 5
39
+ # If sigint hasn't worked after .5 sec try SIGTERM
40
+ fx.kill!("TERM")
41
+ end
42
+ sleep 0.1
43
+ end
data/lib/ps.rb CHANGED
@@ -49,7 +49,7 @@ module PS
49
49
  def from_lsof match, args={}
50
50
  lines = `lsof -i #{match} -sTCP:LISTEN`.chomp.split("\n")
51
51
  lines.shift # remove header
52
-
52
+
53
53
  pids = lines.collect do |line|
54
54
  if m = line.match(/\s*\w+\s+(\d+)/)
55
55
  m[1].to_i
@@ -68,6 +68,8 @@ require 'ps/process_list_printer'
68
68
 
69
69
  def PS *args
70
70
  case args[0]
71
+ when /\:\d+$/
72
+ PS.from_lsof(*args)
71
73
  when Regexp
72
74
  opts = args[1] || {}
73
75
  procs = PS.all(opts)
@@ -75,10 +77,16 @@ def PS *args
75
77
  procs = procs.select {|proc| proc.pid != Process.pid} unless opts[:include_self]
76
78
  procs
77
79
  when Integer
78
- PS.pid(*args).first
80
+ if args[1].is_a?(Integer)
81
+ PS.pid(*args)
82
+ else
83
+ PS.pid(*args).first
84
+ end
85
+ when Array
86
+ pids = args[0]
87
+ pids << args[1] || {}
88
+ PS.pid(*pids)
79
89
  when Hash
80
90
  PS.all(*args)
81
- when /\:\d+$/
82
- PS.from_lsof(*args)
83
91
  end
84
92
  end
@@ -42,7 +42,7 @@ module PS
42
42
 
43
43
  def alive?
44
44
  return unless pid
45
- Process.getpgid(pid)
45
+ ::Process.getpgid(pid)
46
46
  true
47
47
  rescue Errno::ESRCH
48
48
  false
@@ -28,6 +28,8 @@ module PS
28
28
  to.send :puts, printer.to_s
29
29
  end
30
30
 
31
+ # Print out all the processes and accept user input to return
32
+ # the process objects selected.
31
33
  def choose question=nil
32
34
  if empty?
33
35
  puts "No processes found."
@@ -76,7 +78,7 @@ module PS
76
78
  # it to work any other way.
77
79
  def method_missing(*args, &blk)
78
80
  new_target = @target.send(*args, &blk)
79
-
81
+
80
82
  new_target.class == @target.class ? self.class.new(new_target) : new_target
81
83
  end
82
84
  end
@@ -1,3 +1,3 @@
1
1
  module PS
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
metadata CHANGED
@@ -1,64 +1,60 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: ps
3
- version: !ruby/object:Gem::Version
4
- hash: 17
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.8
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 7
10
- version: 0.0.7
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Tal Atlas
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-12-01 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2012-08-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: rspec
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 3
29
- segments:
30
- - 0
31
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
32
22
  type: :development
33
- version_requirements: *id001
34
- - !ruby/object:Gem::Dependency
35
- name: ansi
36
23
  prerelease: false
37
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: ansi
32
+ requirement: !ruby/object:Gem::Requirement
38
33
  none: false
39
- requirements:
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- hash: 3
43
- segments:
44
- - 0
45
- version: "0"
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
46
38
  type: :runtime
47
- version_requirements: *id002
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
48
46
  description: A ruby utility for interacting with the unix tool 'ps'
49
- email:
47
+ email:
50
48
  - me@tal.by
51
49
  executables: []
52
-
53
50
  extensions: []
54
-
55
51
  extra_rdoc_files: []
56
-
57
- files:
52
+ files:
58
53
  - .gitignore
59
54
  - .irbrc
60
55
  - .rspec
61
56
  - Gemfile
57
+ - README.md
62
58
  - Rakefile
63
59
  - lib/ps.rb
64
60
  - lib/ps/command.rb
@@ -73,39 +69,29 @@ files:
73
69
  - test
74
70
  homepage: https://github.com/Talby/ps
75
71
  licenses: []
76
-
77
72
  post_install_message:
78
73
  rdoc_options: []
79
-
80
- require_paths:
74
+ require_paths:
81
75
  - lib
82
- required_ruby_version: !ruby/object:Gem::Requirement
76
+ required_ruby_version: !ruby/object:Gem::Requirement
83
77
  none: false
84
- requirements:
85
- - - ">="
86
- - !ruby/object:Gem::Version
87
- hash: 3
88
- segments:
89
- - 0
90
- version: "0"
91
- required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
83
  none: false
93
- requirements:
94
- - - ">="
95
- - !ruby/object:Gem::Version
96
- hash: 3
97
- segments:
98
- - 0
99
- version: "0"
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
100
88
  requirements: []
101
-
102
89
  rubyforge_project: ps
103
- rubygems_version: 1.8.8
90
+ rubygems_version: 1.8.24
104
91
  signing_key:
105
92
  specification_version: 3
106
93
  summary: A ruby wrapper for the unix tool 'ps'
107
- test_files:
94
+ test_files:
108
95
  - spec/command_spec.rb
109
96
  - spec/ps_spec.rb
110
97
  - spec/spec_helper.rb
111
- has_rdoc: