Class: Die

Inherits:
Object
  • Object
show all
Defined in:
lib/die.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Die

Returns a new instance of Die.



29
30
31
32
33
34
35
# File 'lib/die.rb', line 29

def initialize(opts={})
  self.signal = (opts[:signal] || "KILL").upcase
  if !Signal.list.has_key?(self.signal)
    raise Exception.new "Unknown signal #{self.signal}. Available signals: #{Signal.list.keys.sort.join(", ")}"
  end
  self.search = opts[:search] || ""
end

Instance Attribute Details

#searchObject

Returns the value of attribute search.



4
5
6
# File 'lib/die.rb', line 4

def search
  @search
end

#signalObject

Returns the value of attribute signal.



4
5
6
# File 'lib/die.rb', line 4

def signal
  @signal
end

Class Method Details

.helpObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/die.rb', line 76

def self.help
  <<-STR
Description
Kill (or send a different signal to) one or more processes by search string. 
Multiple search strings can be provided, separated by a space.  
The default signal is KILL; a different one can be provided with the
-s switch (see below).

Usage
#{$0} search_string1 [...search_string2 [...]]

Available signals
#{Signal.list.keys.join(", ")}

Options
  STR
end

.run_from_optionsObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/die.rb', line 59

def self.run_from_options
  opts = {}
  o = OptionParser.new do |opt|
    opt.banner = self.help
    opt.on("-v", "--version", "Show version") do
      $stdout.puts "#{self.to_s} #{File.read(File.join(File.dirname(__FILE__), '..', 'VERSION'))}"
      return false
    end
    opt.on("-s", "--signal SIGNAL", "Signal (defaults to KILL)") do |sig|
      opts[:signal] = sig
    end
  end
  o.parse!
  opts[:search] = ARGV[0..-1]
  self.new(opts).run
end

Instance Method Details

#kill_phash_entry(entry) ⇒ Object



6
7
8
9
10
# File 'lib/die.rb', line 6

def kill_phash_entry(entry)
  pid = entry[0].strip.to_i
  Process.kill(self.signal, pid)
  $stdout.puts "#{self.signal} #{pid} #{entry[1]}"
end

#process_hashObject

1 =>	["1545", "/Applications/Firefox.app/Contents/MacOS/firefox-bin -psn_0_139298"],
2 =>	["1546", "/Applications/Firefox.app/Contents/MacOS/firefox-bin -psn_0_139298"]



20
21
22
23
24
25
26
27
# File 'lib/die.rb', line 20

def process_hash
  p_hash = {}
  processes.each_with_index do |p,i|
    p_hash[i+1] = p.split
    processes[i] = "#{i+1}.\t#{p[0..150]}"
  end
  p_hash
end

#processesObject



12
13
14
# File 'lib/die.rb', line 12

def processes
  @processes ||= `ps -ae -o pid,command | egrep -i '(#{self.search.join("|")})' | egrep -v \"(#{$0}|grep)\"`.split("\n").map(&:strip)
end

#runObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/die.rb', line 37

def run
  unless processes.empty?
    p_hash = process_hash
    $stdout.puts
    $stdout.puts processes
    $stdout.puts
    $stdout.puts " Type 'all' to #{self.signal} 'em all, or the numbers (separated by a space) to kill some. Type anything else to quit."
    inp = $stdin.gets.strip
    if(inp =~ /^a(ll)?/i)
      p_hash.each do |k,v|
        kill_phash_entry(v)
      end
    elsif(inp =~ /[\d\s]{1,}/)
      inp.split.each do |num|
        kill_phash_entry(p_hash[num.to_i])
      end
    end
  else
    $stdout.puts "No processes matching #{ARGV[0]}"
  end
end