Monit + RVM + Thin on OSX / Linux

After several hours (as well as trying God and Bluepill), I decided to ask my question here, because I absolutely do not know how to solve this problem.

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

I have the following monit file, which supposedly does what I want but does not:

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 just copy / paste the start program into the command line (outside of Monit), it works. The same thing happens for the stop program , then to stop the thin instance. However, running it through Monit does not seem to work.

Running in -v verbose mode results in the following:

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

It makes me think that Thin never initializes. Does Monit work as root or something else? Because if this happens, he obviously will not have the correct gems installed, since I use RVM, not the Ruby β€œsystem”. I am on OSX (but eventually deploying to Linux) - does anyone know what is the reason for this? And if Monit runs as root, how can I use RVM independently? Or can I say that Monit executed start / stop programs like Michael:staff (do I assume it will be on OSX?)

Any help is much appreciated!

+7
source share
3 answers

monit cleans up the environment and also does not launch a shell for your command (not to mention interactive). I believe that I need 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 a monit launch command.

+11
source

another parameter that I found in the google RVM group is as follows:

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

su - the user launches the user shell as the login shell, so if the user shell is bash, this will trigger ~ / .bash_profile so that the environment variables should be the same as the user logged in immediately after.

We need a path for su, otherwise monitrc could not find the su executable.

+7
source

It is best to use the RVM wrapper to create a custom executable for thin ones. It will create the right environment variables to use the right ruby ​​and gemstones, and then become subtle. Read more about this here with God: https://rvm.io/integration/god/ . It should work with monit

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

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

+4
source

All Articles