Monday, January 17, 2011

God (process monitoring) RVM + init script

RVM
Here's how I got god working with rvm.
1. Install rvm for the system.
http://rvm.beginrescueend.com/deployment/system-wide/

2. Set up ruby and gemset.  Note that god doesn't play nicely with ruby-1.9.2-p136 but should play nice with the next release of 1.9.x.
rvm install ree
rvm use ree
rvm gemset create system
rvm use ree@system

3. Install god
gem install god

4. Create wrapper
rvm wrapper ree@system boot god

5. Make sure your init script uses /usr/local/rvm/bin/boot_god or use mine below.


6. Set default ruby.
For the system ruby:
rvm --default use system

For ree:
rvm --default use ree


7. Create your /etc/god.conf.  Make sure that you use rvm to wrap any binaries you need to run with a particular version of ruby or a particular set of gems (delayed_job is a good example).

Init Script
I wasn't happy with any of the init scripts I could find for God so I wrote my own.
This works with Fedora, RHEL, CentOS.

 #!/bin/bash
 #
 # God
 #
 # chkconfig: 2345 85 15
 # description: start, stop, restart God
 #
 # Also consider adding this line (kills god weekly) to your crontab (sudo crontab -e):
 #
 #    # deicide is painless
 #    0 1 * * 0 god quit; sleep 1; killall god; sleep 1; killall -9 god; sleep 1; /etc/init.d/god start
 #

 . /etc/rc.d/init.d/functions

 RETVAL=0
 prog="god"

 CONF="/etc/god.conf"
 PID_FILE="/var/run/god/god.pid" ; mkdir -p `dirname $PID_FILE`
 LOG_FILE="/var/log/god/god.log" ; mkdir -p `dirname $LOG_FILE`
 GOD="/usr/local/rvm/bin/boot_god"

 start()
 {
   echo -n $"Starting $prog: "
   $GOD -c "$CONF" -P "$PID_FILE" -l "$LOG_FILE" && success || failure
   RETVAL=$?
   echo
 }

 stop()
 {
   echo -n $"Stopping $prog: "
   kill `cat $PID_FILE` && success || failure
   RETVAL=$?
   echo
 }

 case "$1" in
     start)
       start
   ;;
     stop)
       stop
   ;;
     restart)
       stop
       start
   ;;
     status)
       status -p $PID_FILE $prog
       RETVAL=$?
   ;;
     *)
       echo "Usage: $prog {start|stop|restart|status}"
       exit 1
   ;;
 esac

 exit $RETVAL