Module: PS

Extended by:
PS
Included in:
PS
Defined in:
lib/ps.rb,
lib/ps/command.rb,
lib/ps/process.rb,
lib/ps/version.rb,
lib/ps/process_list.rb,
lib/ps/process_list_printer.rb

Defined Under Namespace

Classes: Command, Process, ProcessList

Constant Summary collapse

ALL_FORMATS =
`ps -L`.chomp.split(/[\s\n]/).freeze
FORMAT_ALIASES =
{
  'pcpu' => '%cpu',
  'pmem' => '%mem',
  'acflg' => 'acflag',
  'f' => 'flags',
  'group' => 'gid',
  'inblock' => 'inblk',
  'ni' => 'nice',
  'nsignals' => 'nsigs',
  'oublock' => 'oublk',
  'pending' => 'sig',
  'blocked' => 'sigmask',
  'stat'    => 'stage',
  'cputime' => 'time',
  'usrpri' => 'upr',
  'putime' => 'utime',
  'vsize' => 'vsz'
}
DEFAULT_FORMATTING =
%w{pid ppid pgid rss vsz %mem %cpu ruser
user uid gid lstart state command}
VERSION =
"0.0.8"

Instance Method Summary collapse

Instance Method Details

#all(opts = {}) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/ps.rb', line 29

def all opts={}
  opts ||= {}
  opts[:flag] ||= %w{A}
  opts[:format] ||= DEFAULT_FORMATTING

  c = Command.new(opts)
  c.to_processes
end

#from_lsof(match, args = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ps.rb', line 49

def from_lsof match, args={}
  lines = `lsof -i #{match} -sTCP:LISTEN`.chomp.split("\n")
  lines.shift # remove header

  pids = lines.collect do |line|
    if m = line.match(/\s*\w+\s+(\d+)/)
      m[1].to_i
    end
  end.compact

  pids << args
  pid(*pids)
end

#pid(*pids) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/ps.rb', line 38

def pid *pids
  opts = pids.pop if pids.last.is_a?(Hash) || pids.last.nil?
  opts ||= {}
  opts[:flag] ||= %w{}
  opts[:pid] = pids
  opts[:format] ||= DEFAULT_FORMATTING

  c = Command.new(opts)
  c.to_processes
end