die 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (6) hide show
  1. data/VERSION +1 -1
  2. data/bin/die +1 -1
  3. data/die.gemspec +2 -2
  4. data/lib/die.rb +51 -5
  5. data/test/test_die.rb +22 -0
  6. metadata +4 -4
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
data/bin/die CHANGED
@@ -2,4 +2,4 @@
2
2
  $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
3
3
  require 'die'
4
4
 
5
- Die.new.run
5
+ Die.run_from_options
data/die.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{die}
8
- s.version = "0.1.0"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Billy Reisinger"]
12
- s.date = %q{2011-11-07}
12
+ s.date = %q{2011-11-08}
13
13
  s.default_executable = %q{die}
14
14
  s.description = %q{Kill one or more processes by command name via an interactive script. Die will find processes that match a given command name, and give you the opportunity to kill one, several, all, or none of them.}
15
15
  s.email = %q{billy.reisinger@gmail.com}
data/lib/die.rb CHANGED
@@ -1,12 +1,16 @@
1
+ require 'optparse'
2
+
1
3
  class Die
4
+ attr_accessor :signal, :search
5
+
2
6
  def kill_phash_entry(entry)
3
7
  pid = entry[0].strip.to_i
4
- Process.kill("KILL", pid)
5
- puts "Killed #{pid} #{entry[1]}"
8
+ Process.kill(self.signal, pid)
9
+ puts "#{self.signal} #{pid} #{entry[1]}"
6
10
  end
7
11
 
8
12
  def processes
9
- @processes ||= `ps -ae -o pid,command | grep -i #{ARGV[0]} | egrep -v "(#{$0}|grep)"`.split("\n").map(&:strip)
13
+ @processes ||= `ps -ae -o pid,command | egrep -i '(#{self.search.join("|")})' | egrep -v \"(#{$0}|grep)\"`.split("\n").map(&:strip)
10
14
  end
11
15
 
12
16
  # {
@@ -22,14 +26,21 @@ class Die
22
26
  p_hash
23
27
  end
24
28
 
29
+ def initialize(opts={})
30
+ self.signal = (opts[:signal] || "KILL").upcase
31
+ if !Signal.list.has_key?(self.signal)
32
+ raise Exception.new "Unknown signal #{self.signal}. Available signals: #{Signal.list.keys.sort.join(", ")}"
33
+ end
34
+ self.search = opts[:search] || ""
35
+ end
36
+
25
37
  def run
26
38
  unless processes.empty?
27
39
  p_hash = process_hash
28
-
29
40
  puts
30
41
  puts processes
31
42
  puts
32
- puts " Type 'all' to kill 'em all, or the numbers (separated by a space) to kill some. Type anything else to quit."
43
+ puts " Type 'all' to #{self.signal} 'em all, or the numbers (separated by a space) to kill some. Type anything else to quit."
33
44
  inp = $stdin.gets.strip
34
45
  if(inp =~ /^a(ll)?/i)
35
46
  p_hash.each do |k,v|
@@ -44,4 +55,39 @@ class Die
44
55
  puts "No processes matching #{ARGV[0]}"
45
56
  end
46
57
  end
58
+
59
+ def self.run_from_options
60
+ opts = {:signal => "KILL", :search => ARGV[0..-1]}
61
+ o = OptionParser.new do |opt|
62
+ opt.banner = self.help
63
+ opt.on("-v", "--version", "Show version") do
64
+ puts "#{self.to_s} #{File.read(File.join(File.dirname(__FILE__, '..', 'VERSION')))}"
65
+ exit
66
+ end
67
+ opt.on("-s", "--signal SIGNAL", "Signal (defaults to KILL)") do |sig|
68
+ puts sig, '00' * 100
69
+ opts[:signal] = sig
70
+ end
71
+ end
72
+ o.parse!
73
+ self.new(opts).run
74
+ end
75
+
76
+ def self.help
77
+ <<-STR
78
+ Description
79
+ Kill (or send a different signal to) one or more processes by search string.
80
+ Multiple search strings can be provided, separated by a space.
81
+ The default signal is KILL; a different one can be provided with the
82
+ -s switch (see below).
83
+
84
+ Usage
85
+ #{$0} search_string1 [...search_string2 [...]]
86
+
87
+ Available signals
88
+ #{Signal.list.keys.join(", ")}
89
+
90
+ Options
91
+ STR
92
+ end
47
93
  end
data/test/test_die.rb CHANGED
@@ -54,4 +54,26 @@ class TestDie < Test::Unit::TestCase
54
54
  Process.expects(:kill).with("KILL", 249)
55
55
  d.run
56
56
  end
57
+
58
+ def test_different_signal
59
+ d = Die.new(:signal => "ABRT")
60
+ d.stubs(:processes).returns([
61
+ "42 /usr/sbin/httpd -D FOREGROUND",
62
+ "249 /usr/sbin/httpd -D FOREGROUND"
63
+ ])
64
+ d.stubs(:process_hash).returns({
65
+ 1 => ["42", "/usr/sbin/httpd", "-D", "FOREGROUND"],
66
+ 2 => ["249", "/usr/sbin/httpd", "-D", "FOREGROUND"]
67
+ })
68
+ $stdin.stubs(:gets).returns("1 2")
69
+ Process.expects(:kill).with("ABRT", 42)
70
+ Process.expects(:kill).with("ABRT", 249)
71
+ d.run
72
+ end
73
+
74
+ def test_exception_on_unknown_signal
75
+ assert_raise(Exception) do
76
+ Die.new(:signal => "DONKEY")
77
+ end
78
+ end
57
79
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: die
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Billy Reisinger
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-11-07 00:00:00 -06:00
18
+ date: 2011-11-08 00:00:00 -06:00
19
19
  default_executable: die
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency