Class: KillAll

Inherits:
Object show all
Defined in:
lib/kill_all.rb

Constant Summary collapse

PS =
'ps'.to_ocmd

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#regexpObject

Returns the value of attribute regexp.



11
12
13
# File 'lib/kill_all.rb', line 11

def regexp
  @regexp
end

#signalObject

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