Executing RVM Rubies with Monit

Process monitoring is a must tool if you’re running a must not stopped process as a complement of your application or even the application process itself. In this case I talk about Ruby processes in more specific scope. Let’s say you have a Thin process that serve the main application and a common ruby processes that provide a support service to the main application. Also we might need to monitor another infrastructure like MySQL, Redis or another.

I decided to use Monit since I have to monitor system processes too, another reason is that Monit having it’s own web interface that really takes no pain to configure. I might choose God combined with Jesus (I’m not trying to be religious here) or Bluepill which are written on Ruby, but monit configuration is pretty straight forward to write and understand.

If you’re using RVM (I believe everybody would use this kind of this version manager or another), then you should be carefull when controlling the process to get correct its ruby and gems environment.

Let’s say you’ve already wrote the main Monit configuration and you want to monitor and control a Thin process that using a specific RVM version. This is a sample configuration you put in /etc/monit.d/main-server:

check process mainserver
 with pidfile /home/dimas/apps/main-server/tmp/pids/thin.pid
 group appserver
 start program = "/bin/bash -c 'export rvm_path=/home/dimas/.rvm; . $rvm_path/scripts/rvm; cd /home/dimas/apps/main-server; $rvm_path/gems/ruby-1.9.2-p290@main-server/bin/thin --max-persistent-conns 1024 --timeout 1 -V -p 9999 -d -R config.ru start'"
 stop program = "/bin/bash -c 'export rvm_path=/home/dimas/.rvm; . $rvm_path/scripts/rvm; cd /home/dimas/apps/main-server; $rvm_path/gems/ruby-1.9.2-p290@main-server/bin/thin stop'"
 if failed host localhost port 9999 then restart

We use bash to execute these respective command:
1. Export rvm path to execution environment. Replace the /bin/bash with your system shell if needed, in this case I’m using Ubuntu server 10.04.
2. Load RVM. If you didn’t created .rvmrc configuration file on the application root directory, then you will need to add something like rvm use 1.9.2-p290@main-server or $rvm_path/bin/rvm rvmrc load after this . $rvm_path/scripts/rvm;
3. Run the command with a confidence.

Tags: ,

blog comments powered by Disqus