class Sys::ProcessInfo

Contains all the information that PS can report about a process for the current platform.

The following attribute accessor methods are available:

pid     (integer)
command (string -- the 'ps' name)
name    (alias for 'command')
pcpu    (float)
pmem    (float)
stat    (string)
rss     (integer)
vsz     (integer)
user    (string)
majflt  (integer)
minflt  (integer)
state   (array of symbols; see DARWIN_STATES or LINUX_STATES)

Only on linux:

exename (string -- path to the binary)
fds     (array -- list of open file descriptors)

Constants

DARWIN_STATES
LINUX_STATES

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/epitools/sys/ps.rb, line 127
def initialize(*args)
  @dead = false
  args << stat_to_state(args[PS_FIELDS.index(:stat)])
  super(*args)
end

Public Instance Methods

children() click to toggle source
# File lib/epitools/sys/ps.rb, line 137
def children
  @@parents ||= Sys.ps.group_by(&:ppid)
  @@parents[pid]
end
dead?() click to toggle source

Has this process been killed?

# File lib/epitools/sys/ps.rb, line 161
def dead?
  @dead ||= Sys.pid(pid).empty?
end
exename() click to toggle source
# File lib/epitools/sys/ps.rb, line 186
def exename
  @exename ||= File.readlink("/proc/#{pid}/exe") rescue :unknown
  @exename == :unknown ? nil : @exename
end
fds() click to toggle source
# File lib/epitools/sys/ps.rb, line 191
def fds
  Dir["/proc/#{pid}/fd/*"].map { |fd| File.readlink(fd) rescue nil }
end
kill!(signal="TERM") click to toggle source

Send the TERM signal to this process.

# File lib/epitools/sys/ps.rb, line 152
def kill!(signal="TERM")
  puts "Killing #{pid} (#{signal})"
  Process.kill(signal, pid)
  # TODO: handle exception Errno::ESRCH (no such process)
end
parent() click to toggle source
# File lib/epitools/sys/ps.rb, line 133
def parent
  Sys.ps(ppid).first unless ppid < 1
end
refresh() click to toggle source

Refresh this process’ statistics.

# File lib/epitools/sys/ps.rb, line 168
def refresh
  processes = Sys.ps(pid)

  if processes.empty?
    @dead = true
    raise ProcessNotFound
  end

  updated_process = processes.first
  members.each { |member| self[member] = updated_process[member] }
  self
end
to_hash() click to toggle source

Convert all the process information to a hash.

# File lib/epitools/sys/ps.rb, line 145
def to_hash
  Hash[ *members.zip(values).flatten(1) ]
end

Private Instance Methods

stat_to_state(str) click to toggle source
# File lib/epitools/sys/ps.rb, line 199
def stat_to_state(str)
  states = case Sys.os
    when "Linux"  then LINUX_STATES
    when "Darwin" then DARWIN_STATES
    else raise "Unsupported platform: #{Sys.os}"
  end

  str.scan(/./).map { |char| states[char] }.compact
end