vhv8aw
Last Updated: February 25, 2016
·
11.59K
· dsci
32b6de7d5abfe436f9b7e86c50f73eae

Getting Ruby scripts working with bundler, rvm and cron

If you want the run some Ruby code by cron, that is wrapped into a shell script and uses bundler you will maybe see something like this:

cannot load such file -- bundler/setup

This will never occur if it's running from the command line.

After struggling with it and dangerous superficial knowledge about cron I found a solution that works very well for me.

Update:

You should read anything after the Update section to know why the above mentioned problem occurs!

The lines below are working great if you haven't any specific gemsets to use. If you have, you have
to proceed with GEMHOME and GEMPATH environment variables the same way you will do it with PATH.
A more better solution that isn't mentioned at the RVM homepage in their cron section is the following:

rvm use .... # select the ruby you want to use
rvm cron setup # let RMV do your cron settings
crontab -e  # add a new cronjob

-- end of the update section.

Cron passes very few informations about the environment to the scripts runned by itself. To see what is passed try it by adding a new job with crontab -e</code>.

* * * * *  set > ~/tmp/setvals

Wait a while and then take a look at ~/tmp/setvals</code>

HOME='/home/brewster'
IFS=''
LANG='en_US.UTF-8'
LANGUAGE='en_US:en'
LC_ALL='en_US.UTF-8'
LOGNAME='brewster'
OPTIND='1'
PATH='/usr/bin:/bin'
PPID='23152'
PS1='$ '
PS2='> '
PS4='+ '
PWD='/home/brewster'
SHELL='/bin/sh'

With that informations it's very clear that the script will never have any access to the Ruby executable given by rvm.

The solution is to add a custom PATH environment configuration to the cronfile - if you want to have this behaviour globally working for all your scripts.

To get the used rvm Ruby, try

rvm env --path

in your console and then add to the top of your cronfile (crontab -e</code>):

PATH=/usr/local/rvm/gems/ruby-1.9.3-p392/bin:/usr/local/rvm/gems/ruby 1.9.3p392@global/bin:/usr/local/rvm/rubies/ruby-1.9.3p392/bin:/usr/local/rvm/bin:/home/brewster/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin

Or whatever you want to have in PATH</code>. (I installed bundler</code> in my global</code> gemset.)

Say Thanks
Respond

6 Responses
Add your response

8026
42560b40c451f0bf1941764b939cfa68

Actually there are some recommendation for setting up cron in RVM docs: http://rvm.io/integration/cron

over 1 year ago ·
8028
32b6de7d5abfe436f9b7e86c50f73eae

I know this recommendations, but they didn't work for me as I expected, so I have to use the workaround I described above.

over 1 year ago ·
8035
Gavatar big

To be hones, I really liked the content, but the title almost made me not read it... just FYI. Sounded like a rant ;)

over 1 year ago ·
8080
32b6de7d5abfe436f9b7e86c50f73eae

@kjellski - Changed the title to something not-so-rant-like. ;-)

over 1 year ago ·
8084
Gavatar big

great! sounds more like the goodness it provides :)

over 1 year ago ·
15389
C42f53f2d182cba138d060ad54e12716

Okay, I tried it the hard way by coding PATH myself. Then I got smart and decided to just let "rvm cron setup" do it for me. Worked like a charm. Thanks!

over 1 year ago ·