Last active
August 29, 2015 14:03
-
-
Save kitofr/1fcd3e7659a66b7e376f to your computer and use it in GitHub Desktop.
A simple script to kill off "hanging" ruby processes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ruby %~dp0kill_ruby_processes.rb |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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