Skip to content

Instantly share code, notes, and snippets.

@nesquena
Created April 6, 2011 02:52
Show Gist options
  • Save nesquena/905040 to your computer and use it in GitHub Desktop.
Save nesquena/905040 to your computer and use it in GitHub Desktop.
Kills all processes matching a pattern (killmatch)
# !/usr/bin/env ruby
# USAGE
# sudo killmatch Rails
unless ARGV[0]
puts "Specify a pattern to kill! i.e killmatch Rails"
exit
end
pids = `ps -ef | grep -vE \"^USER|grep|killmatch\" | grep #{ARGV[0]}" | awk '{print $2;}`.chomp.split("\n")
pids.each do |pid|
puts "Killing process #{ARGV[0]} #{pid}'"
system "kill -9 #{pid}"
end
#!/usr/bin/env ruby
# USAGE
# sudo killmatch Rails
unless ARGV[0]
puts "Specify a pattern to kill! i.e killmatch Rails"
exit
end
processes = `ps -ef | grep #{ARGV[0]}`.split("\n").map { |s| s.gsub(/\s+/, ' ').strip }
processes.reject! { |p| p =~ /killmatch/ || p =~ /grep/ || p =~ /ps -ef/ }
process_data = processes.map { |p| p.split(" ") }
process_data.each do |pd|
puts "Killing process '#{pd[0]}' with pid '#{pd[1]}'"
system("kill -9 #{pd[1]}")
end
@DAddYE
Copy link

DAddYE commented Apr 6, 2011

ps -ef | grep -vE "^USER|grep" | grep "Rails"

grep -vE "^USER|grep|killmatch" prevent to show you in the process list the "grep/killmatch command"

@DAddYE
Copy link

DAddYE commented Apr 6, 2011

Another way:

pids = `ps -ef | grep -vE \"^USER|grep|killmatch\" | grep #{ARGV[0]}" | awk '{print $2;}`.chomp.split("\n")
pids.each do |pid|
  puts "Killing process #{ARGV[0]} #{pid}'"
  system "kill -9 #{pid}"
end

@nesquena
Copy link
Author

nesquena commented Apr 6, 2011

That's handy. Thanks Dave.

@DAddYE
Copy link

DAddYE commented Apr 6, 2011

;)

@DAddYE
Copy link

DAddYE commented Apr 6, 2011

There isn't an option in passanger to kill process after they are idle for XX time? I've the same problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment