简体   繁体   中英

Monitoring Ruby script, using Monit - Including RVM

Im using Monit to monitor a ruby script that uses Ruby daemons gem , which launches a separate process with PID - following the instructions from Monitor ruby processes with Monit

In order to execute the ruby script I need to include RVM in the Monit start and stop strings, so I have access to all the gems.

However when .monitrc executes I get the following error:

$rvm_path (/usr/local/rvm) does not exist./home/william/.rvm/scripts/rvm: line 174: rvm_is_a_shell_function: command not found
/home/william/.rvm/scripts/rvm: line 185: __rvm_teardown: command not found
'myserver_1' failed to start
Aborting event

I added PATH=$PATH:/home/william/.rvm/bin && . /home/william/.rvm/scripts/rvm PATH=$PATH:/home/william/.rvm/bin && . /home/william/.rvm/scripts/rvm to the start and stop command strings to try and include RVM. However still it doesn't work

Config file .monitrc :

....

check process myserver_1
  with pidfile /home/william/ruby/barclays/myapp.rb.pid
  start = "/bin/bash -c 'PATH=$PATH:/home/william/.rvm/bin && . /home/william/.rvm/scripts/rvm && ruby /home/william/ruby/barclays/daemonloader.rb start'"
  stop = "/bin/bash -c 'PATH=$PATH:/home/william/.rvm/bin && . /home/william/.rvm/scripts/rvm && ruby /home/william/ruby/barclays/daemonloader.rb stop'"

  ....

Thanks for your help.

EDIT

Ive got a feeling the problem is related to environment variables. Quoting from this page

You should also know that for security reasons Monit purges the environment and only sets a spartan PATH variable that contains /bin, /usr/bin, /sbin and /usr/sbin. If your program or script dies, the reason could be that it expects certain environment variables or to find certain programs via PATH. If this is the case you should set the environment variables you need directly in the start or stop script called by monit.

Finally, Monit uses the system call execv to execute a program or a script. This means that you cannot write shell commands directly in the start, stop or exec statements. To do this, you must do as above; start a shell and issue your commands there. For example:

start program = "/bin/bash -c 'my shell command && my other command'"

Use this:

/path/to/rvm/bin/rvm in /path/to/project do ...

Replace the paths with proper directories for rvm and project and the ... with the commands to stop/start - try:

/usr/bin/env "HOME=/home/william rvm_path=/home/william/.rvm 
  /home/william/.rvm/bin/rvm in /home/william/ruby/project do
  ruby daemonloader.rb start"

This command will load RVM, cd into project path, load ruby for this ruby and execute given command.

You could try something like this in Monit.

start = "/bin/su - william -c 'cd /home/william/ruby/project && ~/.rvm/bin/rvm default do bundle exec ruby daemonloader.rb start'"

This worked for me.

提到 gemset 和 ruby​​ 源为我解决了这个问题。

start program = "/bin/bash -c 'cd /home/project_path && source /home/user/.rvm/environments/ruby-2.4.2@global && RAILS_ENV=production bundle exec rails s'"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM