Class KillAll
In: lib/kill_all.rb
Parent: Object

Methods

[]   call   new  

Constants

PS = 'ps'.to_ocmd

Attributes

regexp  [RW] 
signal  [RW] 

Public Class methods

[Source]

# File lib/kill_all.rb, line 15
  def initialize ( aSignal='KILL' )
    @signal = aSignal
    @pids = []
  end

Public Instance methods

[]( aRegexp )

Alias for call

[Source]

# 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

[Validate]