I finally got monit to keep this blog up and running

After a long struggle, I got monit to restart my webrick when it gets too big. I started by switching back to ruby 1.8.7, which doesn't use multiple pthreads, and it much easier for me to manage. It does use a little more memory, but doesn't spike memory temporarily to high values. I managed to get myself into several monit problems, such as the following image:

htop fully loaded down to 100%

Here's the configuration that ended up working:

check process rails with pidfile /opt/home/blog/tmp/pids/server.pid 
  start program = "/opt/bin/bash -l -c '/opt/etc/init.d/S91rails start'" with timeout 240 seconds
  stop program  = "/opt/bin/bash -l -c '/opt/etc/init.d/S91rails stop'" with timeout 60 seconds
  if cpu > 60% for 2 cycles then alert
  if cpu > 80% for 5 cycles then restart
  if totalmem > 95.0 MB for 2 cycles then restart

check system localhost
  if swap usage > 15% then exec "/opt/bin/killall -9 /opt/local/bin/ruby"

And my startup script S91rails looks like:


#!/opt/bin/bash
# Add the user to /etc/passwd since it disappears on reboot (flashed filesystem is readonly)
AS_USER=blog
grep -q ${AS_USER} /etc/group  > /dev/null || echo "${AS_USER}:x:40:" >> /etc/group
grep -q ${AS_USER} /etc/passwd > /dev/null || echo "${AS_USER}:x:40:40:${AS_USER}:/opt/home/blog:/opt/bin/bash" >> /etc/passwd
# handle comand
case "$1" in
        start)
            su - $AS_USER -c "bash -l -c 'cd /opt/home/blog && nice rails s -d -e production'"
            ;;         
        stop)
            su - $AS_USER -c "bash -l -c 'killall -s INT -q ruby; sleep 10; killall -s KILL -q -w ruby'"
            ;;         
        status)
            su - $AS_USER -c "ps aux | grep 'rails server' >/dev/null || echo 'No rails server running'"
            ;;
        restart)
            stop
            start
            ;;
esac

Published on Thu, 25 Aug 2011 04:03

RSS