Gem Not Found in Ruby Cron Job in Rvm Env

Gem not found in Ruby cron job in RVM env

I configured several different operating systems to work with a couple of CRON flavors and RVM.

I first tried RVM's official solution to the problem but didn't work under FreeBSD and Gentoo. I had to manually add all relevant paths as showed bellow but first type crontab -e in order to launch the crontab editor[1]:

# atmat's crontab configuration
SHELL=/bin/bash
PATH=/home/atma/.rvm/gems/ruby-1.9.3-p0/bin:/home/atma/.rvm/gems/ruby-1.9.3-p0@global/bin:/home/atma/.rvm/rubies/ruby-1.9.3-p0/bin:/home/atma/.rvm/bin:/usr/local/bin:/usr/bin:/bin:/opt/bin:/usr/i486-pc-linux-gnu/gcc-bin/4.5.3
RUBYLIB=/home/atma/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1
GEM_HOME='/home/atma/.rvm/gems/ruby-1.9.3-p0'
GEM_PATH='/home/atma/.rvm/gems/ruby-1.9.3-p0:/home/atma/.rvm/gems/ruby-1.9.3-p0@global'
RUBYOPT=rubygems

%nightly,mail(no) * 8-9 /home/atma/.rvm/rubies/ruby-1.9.3-p0/bin/ruby /usr/local/bin/morula -s username update

The above example is working under Gentoo GNU/Linux using fcron a more flexible, beautiful and powerful solution to standard cron, but will work with any cron.

[1] This command will open crontab with your default system editor.

Gemfile not found when running Cron job with Capistrano 3 and whenever gem

Basically the environment variable is missing that tells the cron where to look for a Gemfile. so you need to add that variable in your environment at the time when cron tries to run this.
You can do that in Your schedule.rb:

env BUNDLE_GEMFILE, ENV["/home/hey_production/current/Gemfile"]

or directly inside crontab file with the command crontab -e(before the cron entries):

BUNDLE_GEMFILE="/home/hey_production/current/Gemfile"  

Hope it helps.

EDIT
Forgot the symbol above in schedule.rb

The line in schedule.rb should be like this.

env :BUNDLE_GEMFILE, ENV["/#{path}/Gemfile"]

or

env :BUNDLE_GEMFILE, ENV["/home/hey_production/current/Gemfile"]

Requiring a Ruby Gem in Ruby Script breaks Cron Job Execution

You need to setup your crontab with rvm e.g:

rvm cron setup

With that rvm sets your environment variables in your crontab file

then you have a crontab file having this at the top:

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'

Then you can stick your crontask beneath it

rails 4.2.0 with whenever gem in development env, not working

Using output in schedule help me to find a problem.

I used specific gemset and not specify it in my schedule.
That's why I got error. Whenever gem trying to use default gemset which was different from gemset that I use for my project.

So there are two options for solving problem, use default gemset or specify yours in schedule.

Sample of setting gemset and env:

set :environment, :development

Rake:

job_type :rake, "cd :path && $HOME/.rvm/scripts/rvm && rvm use
2.2.0@somename && rake ':task' :output"

Runner:

job_type :runner, "cd :path && $HOME/.rvm/scripts/rvm && rvm use 2.2.0@somename && bin/rails runner ':task' :output"



Related Topics



Leave a reply



Submit