Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

3 Answers

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
add comment

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
add comment
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:

myjob.sh

#!/bin/bash

# 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 myjob.sh

crontab

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



BAM! Done.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.