Monit + RVM + Thin on OSX / Linux
Asked Answered
H

3

7

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!

Heated answered 3/5, 2011 at 0:15 Comment(0)
F
11

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.

Filaria answered 24/5, 2011 at 23:27 Comment(1)
Thanks, that's something I was not aware of. :)Heated
G
7

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.

Glottis answered 22/11, 2011 at 19:48 Comment(1)
Finally got this to work with running monit as root and using /bin/su - user... Thanks!Coletta
S
4

A better way would be to use an RVM wrapper to create a custom executable for thin. It will create the correct environment variables to use the right ruby and gems, and then launch thin. Read more about it using it with god here : https://rvm.io/integration/god/. It should work the same with monit

To create the wrapper: rvm wrapper ruby@gemset bootup thin

Then change start program and stop program to use the executable you just created.

Southerly answered 20/8, 2011 at 7:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.