ps 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/ps.rb CHANGED
@@ -3,7 +3,7 @@ require "ps/version"
3
3
  module PS
4
4
  extend self
5
5
 
6
- ALL_OPTS = `ps -L`.chomp.split(/[\s\n]/).freeze
6
+ ALL_FORMATS = `ps -L`.chomp.split(/[\s\n]/).freeze
7
7
  FORMAT_ALIASES = {
8
8
  'pcpu' => '%cpu',
9
9
  'pmem' => '%mem',
@@ -23,11 +23,6 @@ module PS
23
23
  'vsize' => 'vsz'
24
24
  }
25
25
 
26
- def default_formatting
27
- @default_formatting ||= ALL_OPTS
28
- end
29
- attr_writer :default_formatting
30
-
31
26
  DEFAULT_FORMATTING = %w{pid ppid pgid rss vsz %mem %cpu ruser
32
27
  user uid gid lstart state command}
33
28
 
@@ -41,15 +36,29 @@ module PS
41
36
  end
42
37
 
43
38
  def pid *pids
44
- opts = pids.pop if pids.last.is_a?(Hash)
39
+ opts = pids.pop if pids.last.is_a?(Hash) || pids.last.nil?
45
40
  opts ||= {}
46
- opts[:flag] ||= %w{A}
41
+ opts[:flag] ||= %w{}
47
42
  opts[:pid] = pids
48
43
  opts[:format] ||= DEFAULT_FORMATTING
49
44
 
50
45
  c = Command.new(opts)
51
46
  c.to_processes
52
47
  end
48
+
49
+ def from_lsof match, args={}
50
+ lines = `lsof -i #{match} -sTCP:LISTEN`.chomp.split("\n")
51
+ lines.shift # remove header
52
+
53
+ pids = lines.collect do |line|
54
+ if m = line.match(/\s*\w+\s+(\d+)/)
55
+ m[1].to_i
56
+ end
57
+ end.compact
58
+
59
+ pids << args
60
+ pid(*pids)
61
+ end
53
62
  end
54
63
 
55
64
  require 'ps/command'
@@ -59,11 +68,16 @@ require 'ps/process_list'
59
68
  def PS *args
60
69
  case args[0]
61
70
  when Regexp
62
- procs = PS.all(args[1])
63
- procs.select {|proc| proc.command =~ args[0]}
71
+ opts = args[1] || {}
72
+ procs = PS.all(opts)
73
+ procs = procs.select {|proc| proc.command =~ args[0]}
74
+ procs = procs.select {|proc| proc.pid != Process.pid} unless opts[:include_self]
75
+ procs
64
76
  when Integer
65
- PS.pid(args[0],args[1]).first
77
+ PS.pid(*args).first
66
78
  when Hash
67
- PS.all(args[0])
79
+ PS.all(*args)
80
+ when /\:\d+$/
81
+ PS.from_lsof(*args)
68
82
  end
69
83
  end
@@ -76,6 +76,7 @@ module PS
76
76
 
77
77
  def parse_formats!
78
78
  @formats.each do |format|
79
+ format = format.sub('=','')
79
80
  unless DEFAULT_FORMATTING.include?(format)
80
81
  warn("Untested formatting option: #{format}")
81
82
  end
@@ -1,7 +1,7 @@
1
1
  module PS
2
2
  class Process
3
3
 
4
- ALL_OPTS.each do |opt|
4
+ ALL_FORMATS.each do |opt|
5
5
  define_method opt do
6
6
  @data[opt]
7
7
  end
@@ -40,5 +40,30 @@ module PS
40
40
  rss/1024.0 if rss
41
41
  end
42
42
 
43
+ def alive?
44
+ return unless pid
45
+ Process.getpgid(pid)
46
+ true
47
+ rescue Errno::ESRCH
48
+ false
49
+ end
50
+
51
+ def display
52
+ disp = []
53
+ disp << pid
54
+ disp << user
55
+ disp << lstart
56
+ disp << pcpu
57
+ disp << pmem
58
+ disp << mem
59
+ disp << command
60
+ disp.compact.join(' ')
61
+ end
62
+
63
+ def kill!(sig=nil)
64
+ sig ||= 'INT'
65
+ ::Process.kill(sig, pid) if pid
66
+ end
67
+
43
68
  end
44
69
  end
@@ -1,3 +1,3 @@
1
1
  module PS
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ps
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tal Atlas
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-11-28 00:00:00 Z
18
+ date: 2011-11-29 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rspec