Skip to content

Instantly share code, notes, and snippets.

@kitofr
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kitofr/1fcd3e7659a66b7e376f to your computer and use it in GitHub Desktop.
Save kitofr/1fcd3e7659a66b7e376f to your computer and use it in GitHub Desktop.
A simple script to kill off "hanging" ruby processes
ruby %~dp0kill_ruby_processes.rb
require 'win32ole'
wmi = WIN32OLE.connect("winmgmts://")
# http://msdn.microsoft.com/en-us/library/aa394372(v=vs.85).aspx
processes = wmi.ExecQuery("select * from win32_process")
puts "Running ruby processes: "
pids = []
processes.each do |p|
next unless p.name == "ruby.exe"
puts "#{p.ProcessId}: #{p.Description} #{p.WorkingSetSize}" if p.name == "ruby.exe"
pids << { pid: p.ProcessId, process: p }
end
if pids.count == 1
puts "Only myself running atm. Terminating."
exit 0
end
pids.sort { |x,y| x[:pid] <=> y[:pid] }[0..-1].each do |p|
puts " - Killing: #{p[:pid]}"
p[:process].Terminate 8
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment