-
-
Save stevep/4993281 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
#!/bin/bash | |
# | |
# This script is triggered by crontab every 5 minutes. | |
# Logs to /var/log/ui-server/<host>-rails_check.log. | |
log_dir=/var/log/my_rails_app | |
mkdir -p $log_dir | |
log_file=$log_dir/$(hostname)-rails_check.log | |
# Limits in MB | |
SOFT_LIMIT=700 | |
MIDD_LIMIT=900 | |
HARD_LIMIT=1100 | |
for i in `pgrep -f Rack`; do | |
mem=`ps -o rss= -p $i` | |
[ -z "$mem" ] && continue | |
if [ "$mem" -gt $(($HARD_LIMIT*1000)) ]; then | |
echo "$(date) PID $i exceeds hard memory limit (>${HARD_LIMIT}MB)." >> $log_file | |
kill -KILL $i | |
elif [ "$mem" -gt $(($MIDD_LIMIT*1000)) ]; then | |
echo "$(date) PID $i exceeds memory limit (>${MIDD_LIMIT}MB)." >> $log_file | |
kill -TERM $i | |
elif [ "$mem" -gt $(($SOFT_LIMIT*1000)) ]; then | |
echo "$(date) PID $i exceeds soft memory limit (>${SOFT_LIMIT}MB)." >> $log_file | |
kill -USR1 $i | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment