Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to run a rake task using a scheduled cronjob. My crontab looks something like this:

0 1 * * 1-7 /bin/bash -l -c "cd ~/jobs/rake && rake reports:create >> ~/jobs/logs/cron.log"  

Ruby on my account is provided by RVM, which is loaded via ~/.bashrc (before the no-interaction check):

# load RVM env
[[ -s $HOME/.rvm/scripts/rvm ]] && source $HOME/.rvm/scripts/rvm

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# ... rest of logic

Time and again, this task fails to run since RVM isn't loaded when the task is called (uses system's /usr/bin/ruby instead), and gem dependencies are missing.

How can I make crontab load my shell environment before executing my scheduled jobs? thanks.

share|improve this question

using login shell is not recommended in background processes.

you can use this method:

0 1 * * 1-7 ~/.rvm/bin/rvm in ~/jobs/rake do rake reports:create >> ~/jobs/logs/cron.log"

also there is rvm cron command:

rvm help cron
share|improve this answer

Wouldn't simply sourcing your ~/.bashrc in the cronjob work?

0 1 * * 1-7 /bin/bash -l -c "source ~/.bashrc; cd ~/jobs/rake && rake reports:create >> ~/jobs/logs/cron.log"  
share|improve this answer
I thought so too, but that didn't work. – sa125 Aug 20 '12 at 17:57
@sa125 Could that be because of the [ -z "$PS1" ] && return line? I'm pretty sure cronjobs don't run in an interactive shell. Alternatively, could you not put [[ -s $HOME/.rvm/scripts/rvm ]] && source $HOME/.rvm/scripts/rvm into the cron line? – terdon Aug 20 '12 at 18:06
First - I appreciate your help. I tried moving the [[ -s $HOME/.rvm/scripts/rvm ]] && source $HOME/.rvm/scripts/rvm line into it's own file, ~/.loadrvm. I then modified the crontab line to 0 1 * * 1-7 /bin/bash -l -c "source ~/.loadrvm && echo `which ruby` >> ~/cron.log". I still get /usr/bin/ruby. – sa125 Aug 21 '12 at 4:31
@sa125 Could this be because you are starting bash as a login shell (-l) instead of an interactive shell? – terdon Aug 21 '12 at 13:54
up vote 1 down vote accepted

After a little digging following the response from @mpapis, it seems there's a page on the RVM site which elaborates how to use a specific RVM install in a script executed by cron.

Basically you find out which RVM you'd like to use by running this (using ruby 1.9.3 here):

$ rvm env --path -- 1.9.3-p125

Substitute your specific ruby version. This will point to the file that needs to be sourceed so that your desired ruby environment is loaded.

Write the script to be run by your cron job:


# the full path to the RVM env you got from running `rvm env ...`
source ~/.rvm/environments/ruby-1.9.3-p125

# do stuff ...
cd ~/jobs/rake
rake reports:create

Make sure you make the file executable:

chmod u+x


0 1 * * 1-7 bash -c "cd ~/jobs && ./"

BAM! Done.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .