-
-
Save JangoSteve/399967 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 | |
# adapted for RMSR from http://gist.github.com/41713 | |
# run every couple minutes via cron | |
# and output results to script_output.log | |
# sudo crontab -e | |
# then add the following: | |
# */2 * * * * ruby /bin/passenger_monitor_cron 2>&1 >> /var/log/script_output.log | |
command = '/usr/bin/passenger-memory-stats' | |
memory_limit = 200 # megabytes | |
def running?(pid) | |
begin | |
return Process.getpgid(pid) != -1 | |
rescue Errno::ESRCH | |
return false | |
end | |
end | |
`#{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..." | |
sleep 20 | |
if running?(pid.to_i) | |
puts "Process is still running, sending term signal" | |
Process.kill("KILL", pid.to_i) | |
end | |
# remove these two lines after everything checks out | |
else | |
puts "#{Time.now}: Everything ok." | |
end | |
end |
Also, set the memory usage threshold for each Passenger process on line 11. This depends on the size of the server and the number of Passenger processes specified by the PassengerPoolSize. For a 256mb server running MySQL, 50mb is a good starting point. For a 512mb server, try starting with 200 maybe.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Save this file to the following location on your server and make sure it's executable:
/bin/passenger_monitor_cron