Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
891 views
in Technique[技术] by (71.8m points)

bash - Running a gem on cron with a specific version of ruby

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...