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.
127 128 129 130 131 |
# 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 |
Instance Method Details
#children ⇒ Object
137 138 139 140 |
# File 'lib/epitools/sys/ps.rb', line 137 def children @@parents ||= Sys.ps.group_by(&:ppid) @@parents[pid] end |
#dead? ⇒ Boolean
Has this process been killed?
161 162 163 |
# File 'lib/epitools/sys/ps.rb', line 161 def dead? @dead ||= Sys.pid(pid).empty? end |
#exename ⇒ Object
186 187 188 189 |
# File 'lib/epitools/sys/ps.rb', line 186 def exename @exename ||= File.readlink("/proc/#{pid}/exe") rescue :unknown @exename == :unknown ? nil : @exename end |
#fds ⇒ Object
191 192 193 |
# 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") ⇒ Object
Send the TERM signal to this process.
152 153 154 155 156 |
# 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 ⇒ Object
133 134 135 |
# File 'lib/epitools/sys/ps.rb', line 133 def parent Sys.ps(ppid).first unless ppid < 1 end |
#refresh ⇒ Object
Refresh this process’ statistics.
168 169 170 171 172 173 174 175 176 177 178 179 |
# 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 |