Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monit + RVM + Thin on OSX / Linux

Tags:

ruby

rvm

thin

monit

After trying for hours (and also trying God and Bluepill) I decided to ask my question here because I am completely clueless how to solve this issue.

I have a Rails app. I want to use Thin as my app server. I want to use Monit to monitor my Thin instances. I use RVM to manage my Ruby versions as my local user.

I have the following monit file set up that would assumably do what I want it to do, but doesn't:

check process thin-81
  with pidfile /Users/Michael/Desktop/myapp/tmp/pids/thin.81.pid

  start program = "/Users/Michael/.rvm/gems/ruby-1.9.2-p180/bin/thin start -c /Users/Michael/Desktop/myapp -e production -p 81 -d -P tmp/pids/thin.81.pid"
  stop program = "/Users/Michael/.rvm/gems/ruby-1.9.2-p180/bin/thin stop -c /Users/Michael/Desktop/myapp -P tmp/pids/thin.81.pid"

  if totalmem is greater than 150.0 MB for 2 cycles then restart

If I simply copy/paste the start program in to the command line (outside of Monit) it works. Same goes for the stop program to afterwards stop the Thin instance. Running it via Monit however, does not seem to work.

Running it in -v verbose mode yields the following:

monit: pidfile '/Users/Michael/Desktop/myapp/tmp/pids/thin.81.pid' does not exist

Which leads me to believe that Thin never initializes. Does Monit run as root or something? Cause if it does then it obviously won't have the correct gems installed since I'm using RVM and not the "system" Ruby. I am currently on OSX (but will deploy to Linux eventually) - does anyone know what the cause of this might be? And if Monit is run via root, how could I make it use RVM regardless? Or could I tell Monit to execute the start/stop programs as Michael:staff (I assume it would be on OSX?)

Any help is much appreciated!

like image 509
Michael van Rooijen Avatar asked May 03 '11 00:05

Michael van Rooijen


2 Answers

monit clears out the environment and also doesn't run a shell for your command (let alone an interactive one). I find I have to do something like:

/usr/bin/bash -c 'export rvm_path=/home/foo/.rvm; . $rvm_path/scripts/rvm; cd my_ruby_app_path; $rvm_path/bin/rvm rvmrc load; ./my_ruby_app'

as the monit start command.

like image 186
Mark Aufflick Avatar answered Sep 20 '22 23:09

Mark Aufflick


another option which I found in the RVM google group is as follows:

start program = "/bin/su - myuser -c '/path/to/myscript.rb start' " 

su - user runs the user's shell as a login shell, so if the user's shell is bash, it will cause ~/.bash_profile to be run so the environment variables should be the same as just after that user logged in.

We need the path for su, otherwise, monitrc would not able to find the su executable.

like image 36
Francois Avatar answered Sep 20 '22 23:09

Francois