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.
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
* * * * * set > ~/tmp/setvals
Wait a while and then take a look at
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 (
Or whatever you want to have in
PATH</code>. (I installed bundler</code> in my global</code> gemset.)