-
-
Save colin/213684 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'daemons' | |
Daemons.run_proc('PassengerMonitor') do | |
command = 'sudo passenger-memory-stats' | |
memory_limit = 250 | |
def running?(pid) | |
begin | |
return Process.getpgid(pid) != -1 | |
rescue Errno::ESRCH | |
return false | |
end | |
end | |
loop do | |
`#{command}`.each_line do |line| | |
next unless /(\d+)\s+\d+\s+(\d+\.\d+)\s+MB\s+(\d+\.\d+)\s+MB\s+Rails:/.match(line) | |
all, pid, vm_size, private = $~.to_a | |
if private.to_i > memory_limit | |
puts "#{Time.now}: Killing #{pid}, memory usage == #{private}" | |
Process.kill("SIGUSR1", pid.to_i) | |
puts "Finished kill attempt. Sleeping for 20 seconds to give it time to die..." | |
sleep 20 | |
if running?(pid.to_i) | |
puts "Process is still running - die bitch" | |
Process.kill("KILL", pid.to_i) | |
end | |
else | |
puts "Process ##{pid} is not above limit (#{private}MB)" | |
end | |
end | |
sleep 120 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment