Class: Sys::ProcessInfo
Overview
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)
Constant Summary collapse
- DARWIN_STATES =
{ "R"=>:running, "S"=>:sleeping, "I"=>:idle, "T"=>:stopped, "U"=>:wait, "Z"=>:zombie, "W"=>:swapped, "s"=>:session_leader, "X"=>:debugging, "E"=>:exiting, "<"=>:high_priority, "N"=>:low_priority, "+"=>:foreground, "L"=>:locked_pages, }
- LINUX_STATES =
{ "R"=>:running, "S"=>:sleeping, "T"=>:stopped, "D"=>:wait, "Z"=>:zombie, "W"=>:swapped, "X"=>:dead, "s"=>:session_leader, "<"=>:high_priority, "N"=>:low_priority, "+"=>:foreground, "L"=>:locked_pages, "l"=>:multithreaded, }
Instance Method Summary collapse
- #children ⇒ Object
-
#dead? ⇒ Boolean
Has this process been killed?.
- #exename ⇒ Object
- #fds ⇒ Object
-
#initialize(*args) ⇒ ProcessInfo
constructor
A new instance of ProcessInfo.
-
#kill!(signal = "TERM") ⇒ Object
Send the TERM signal to this process.
- #parent ⇒ Object
-
#refresh ⇒ Object
Refresh this process’ statistics.
-
#to_hash ⇒ Object
Convert all the process information to a hash.
Methods inherited from Struct
Constructor Details
#initialize(*args) ⇒ ProcessInfo
Returns a new instance of ProcessInfo.
154 155 156 157 158 |
# File 'lib/epitools/sys.rb', line 154 def initialize(*args) @dead = false args << stat_to_state(args[PS_FIELDS.index(:stat)]) super(*args) end |
Instance Method Details
#children ⇒ Object
164 165 166 167 |
# File 'lib/epitools/sys.rb', line 164 def children @@parents ||= Sys.ps.group_by(&:ppid) @@parents[pid] end |
#dead? ⇒ Boolean
Has this process been killed?
188 189 190 |
# File 'lib/epitools/sys.rb', line 188 def dead? @dead ||= Sys.pid(pid).empty? end |
#exename ⇒ Object
213 214 215 216 |
# File 'lib/epitools/sys.rb', line 213 def exename @exename ||= File.readlink("/proc/#{pid}/exe") rescue :unknown @exename == :unknown ? nil : @exename end |
#fds ⇒ Object
218 219 220 |
# File 'lib/epitools/sys.rb', line 218 def fds Dir["/proc/#{pid}/fd/*"].map { |fd| File.readlink(fd) rescue nil } end |
#kill!(signal = "TERM") ⇒ Object
Send the TERM signal to this process.
179 180 181 182 183 |
# File 'lib/epitools/sys.rb', line 179 def kill!(signal="TERM") puts "Killing #{pid} (#{signal})" Process.kill(signal, pid) # TODO: handle exception Errno::ESRCH (no such process) end |
#parent ⇒ Object
160 161 162 |
# File 'lib/epitools/sys.rb', line 160 def parent Sys.ps(ppid).first unless ppid < 1 end |
#refresh ⇒ Object
Refresh this process’ statistics.
195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/epitools/sys.rb', line 195 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 |