Skip to content

Instantly share code, notes, and snippets.

@bjeanes
Forked from adamcooke/gist:213628
Created October 19, 2009 22:10
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 bjeanes/213775 to your computer and use it in GitHub Desktop.
Save bjeanes/213775 to your computer and use it in GitHub Desktop.
#!/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