Class: KillAll
Constant Summary collapse
- PS =
'ps'.to_ocmd
Instance Attribute Summary collapse
-
#regexp ⇒ Object
Returns the value of attribute regexp.
-
#signal ⇒ Object
Returns the value of attribute signal.
Instance Method Summary collapse
- #call(aRegexp) ⇒ Object (also: #[])
-
#initialize(aSignal = 'KILL') ⇒ KillAll
constructor
A new instance of KillAll.
Constructor Details
#initialize(aSignal = 'KILL') ⇒ KillAll
Returns a new instance of KillAll.
15 16 17 18 |
# File 'lib/kill_all.rb', line 15 def initialize ( aSignal='KILL' ) @signal = aSignal @pids = [] end |
Instance Attribute Details
#regexp ⇒ Object
Returns the value of attribute regexp.
11 12 13 |
# File 'lib/kill_all.rb', line 11 def regexp @regexp end |
#signal ⇒ Object
Returns the value of attribute signal.
10 11 12 |
# File 'lib/kill_all.rb', line 10 def signal @signal end |
Instance Method Details
#call(aRegexp) ⇒ Object Also known as: []
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/kill_all.rb', line 20 def call ( aRegexp ) data = PS['a'].system data.output.open do |ps| ps.readline ps.each do |line| if line =~ /^\s*(\d+)(?:\s+\S+){3}\s+(.*)$/ pid, name = $1.to_i, $2 if name =~ aRegexp and pid != $$ @pids << pid begin Process.kill(@signal, pid) rescue raise RuntimeError, "Cannot kill #{name}:#{pid}" end end else raise RuntimeError, "bad ps output (#{line})" end end end end |