Module: LinuxStat::Process
- Defined in:
- lib/linux_stat/process.rb
Overview
Shows various information about a process that is either running, sleeping, idle or a zombie.
Class Method Summary collapse
-
.cmdlines ⇒ Object
Returns all the id of processes mapped with their cmdline info as a Hash.
-
.count ⇒ Object
Counts and returns the total number of process running on the system.
-
.idle ⇒ Object
Returns all the id of processes that are idle.
-
.list ⇒ Object
Returns the list of processes from /proc/.
-
.names ⇒ Object
Returns all the id of processes mapped with their executable names (comm) as a Hash.
-
.running ⇒ Object
Returns all the id of processes that are running.
-
.sleeping ⇒ Object
Returns all the id of processes that are sleeping.
-
.stopped ⇒ Object
Returns all the id of processes that are stopped.
-
.types ⇒ Object
Returns all the id of processes mapped with their status as a Hash.
-
.zombie ⇒ Object
Returns all the id of processes that are zombies.
Class Method Details
.cmdlines ⇒ Object
Returns all the id of processes mapped with their cmdline info as a Hash. The cmdlines aren’t usually truncated like names, but they can contain arguments with the command.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/linux_stat/process.rb', line 57 def cmdlines h, i = {}, -1 l = list count = l.length while(i += 1) < count x = l[i] begin cmdlines = IO.read("/proc/#{x}/cmdline").strip cmdlines.gsub!(?\u0000.freeze, ?\s.freeze) h.merge!( x => cmdlines) rescue StandardError end end h end |
.count ⇒ Object
Counts and returns the total number of process running on the system.
The return type is Integer.
29 30 31 |
# File 'lib/linux_stat/process.rb', line 29 def count list.length end |
.idle ⇒ Object
Returns all the id of processes that are idle. The return type is an Array of Integers.
124 125 126 127 128 129 130 131 132 |
# File 'lib/linux_stat/process.rb', line 124 def idle list.select { |x| begin IO.read("/proc/#{x}/stat").split(/(\(.*\))/)[-1][/\s.+?/].strip == ?I.freeze rescue StandardError false end } end |
.list ⇒ Object
Returns the list of processes from /proc/.
The return type is an Array of Integers.
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/linux_stat/process.rb', line 11 def list d = Dir['/proc/*'.freeze] ret, i = [], -1 count = d.length while(i += 1) < count pid = File.split(d[i])[1] pid_i = pid.to_i ret << pid_i if pid_i.to_s == pid end ret end |
.names ⇒ Object
Returns all the id of processes mapped with their executable names (comm) as a Hash. The names can be truncated to TASK_COMM_LEN or (16 - 1 = 15) places.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/linux_stat/process.rb', line 36 def names h, i = {}, -1 l = list count = l.length while(i += 1) < count x = l[i] begin h.merge!( x => IO.read("/proc/#{x}/comm").strip) rescue StandardError end end h end |
.running ⇒ Object
Returns all the id of processes that are running. The return type is an Array of Integers.
150 151 152 153 154 155 156 157 158 |
# File 'lib/linux_stat/process.rb', line 150 def running list.select { |x| begin IO.read("/proc/#{x}/stat").split(/(\(.*\))/)[-1][/\s.+?/].strip == ?R.freeze rescue StandardError false end } end |
.sleeping ⇒ Object
Returns all the id of processes that are sleeping. The return type is an Array of Integers.
111 112 113 114 115 116 117 118 119 |
# File 'lib/linux_stat/process.rb', line 111 def sleeping list.select { |x| begin IO.read("/proc/#{x}/stat").split(/(\(.*\))/)[-1][/\s.+?/].strip == ?S.freeze rescue StandardError false end } end |
.stopped ⇒ Object
Returns all the id of processes that are stopped. The return type is an Array of Integers.
163 164 165 166 167 168 169 170 171 172 |
# File 'lib/linux_stat/process.rb', line 163 def stopped list.select { |x| begin v = IO.read("/proc/#{x}/stat").split(/(\(.*\))/)[-1][/\s.+?/].strip v == ?T.freeze || v == ?t.freeze rescue StandardError false end } end |
.types ⇒ Object
Returns all the id of processes mapped with their status as a Hash.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/linux_stat/process.rb', line 78 def types h, i = {}, -1 l = list count = l.length while(i += 1) < count x = l[i] begin h.merge!(x => case IO.read("/proc/#{x}/stat").split(/(\(.*\))/)[-1][/\s.+?/].strip when ?S.freeze then :sleeping when ?I.freeze then :idle when ?Z.freeze then :zombie when ?R.freeze then :running when ?T.freeze then :stopped when ?X.freeze then :dead when ?D.freeze then :sleeping when ?t.freeze then :stopped else :unknown end ) rescue StandardError end end h end |
.zombie ⇒ Object
Returns all the id of processes that are zombies. The return type is an Array of Integers.
137 138 139 140 141 142 143 144 145 |
# File 'lib/linux_stat/process.rb', line 137 def zombie list.select { |x| begin IO.read("/proc/#{x}/stat").split(/(\(.*\))/)[-1][/\s.+?/].strip == ?Z.freeze rescue StandardError false end } end |