Running a gem on cron with a specific version of r

2019-08-17 04:51发布

问题:

We need a certain gem application run on a cron schedule, so we have a dedicated user set up to run this gem.

We are currently using ruby 1.9.2

[jobrunner@test-load] rvm list
rvm rubies

   ruby-1.8.7-p299 [ x86_64 ]
   ruby-1.9.2-p180 [ x86_64 ]
=* ruby-1.9.2-p290 [ x86_64 ]
   ruby-1.9.3-p125 [ x86_64 ]
   ruby-1.9.3-p374 [ x86_64 ]
   ruby-1.9.3-p392 [ x86_64 ]
   ruby-1.9.3-p448 [ x86_64 ]
   ruby-2.0.0-p247 [ x86_64 ]

# => - current
# =* - current && default
#  * - default

Unfortunately I need this user to run on ruby 1.9.3. Changing the default ruby version is not an option because rvm is installed system wide (/usr/local/rvm/bin/rvm) and other users on the system rely on 1.9.2.

If I were in an interactive session, it would be as simple as

rvm use 1.9.3

However, it appears I can't use rvm use in a non-interactive session (i.e. in the crontab). (I tried it by sticking that in my bashrc file and telling cron to look at my bashrc file. I ended up forkbombing myself..)

How do I get my crontab using a specific version of ruby via rvm?

回答1:

From tty switch ruby version:

rvm use 1.9.3

You should set up cron with rvm:

rvm cron setup

then your crontab would look like this:

PATH="/usr/local/rvm/gems/ruby-1.9.3-p194/bin:/usr/local/rvm/gems/ruby-1.9.3-p194@global/bin:/usr/local/rvm/rubies/ruby-1.9.3-p194/bin:/usr/local/rvm/bin:/usr/local/rvm/gems/ruby-1.9.3-p194/bin:/usr/local/rvm/gems/ruby-1.9.3-p194@global/bin:/usr/local/rvm/rubies/ruby-1.9.3-p194/bin:/usr/local/rvm/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/rvm/gems/ruby-1.9.3-p194@global/"
rvm_env_string='ruby-1.9.3-p194'
rvm_path='/usr/local/rvm'
rvm_ruby_string='ruby-1.9.3-p194'
RUBY_VERSION='ruby-1.9.3-p194'
GEM_HOME='/usr/local/rvm/gems/ruby-1.9.3-p194'
GEM_PATH='/usr/local/rvm/gems/ruby-1.9.3-p194:/usr/local/rvm/gems/ruby-1.9.3-p194@global'
MY_RUBY_HOME='/usr/local/rvm/rubies/ruby-1.9.3-p194'
IRBRC='/usr/local/rvm/rubies/ruby-1.9.3-p194/.irbrc'

Cron would use 1.9.3 ruby with the above

EDIT

Just noticed my text was not well formatted on SO, so I have reformatted it



回答2:

You can source the rvm confined ruby from your script like this:

#!/usr/bin/env bash

# load rvm ruby
source /usr/local/rvm/environments/ruby-1.9.3-p448@projectX

# ..... rake, ruby, whatever you need your script to do 


标签: ruby bash rvm