Skip to content

running rake from cron

Quick tip, mostly for myself:

I’m moving a site to a new server. I’ve got several cron jobs set up to do things like delete caches, rotate logs, backup the db, etc. Most of the code for those tasks is written as rake tasks, so it’s nice, readable ruby code and included in the code repository for the project.

The tasks weren’t working on the new server. Here’s a few things I did to track down the problem, from http://www.linuxquestions.org/questions/bsd-17/crontab-test-550745/: ps ax | grep cron : yep, cron is running * * * * * /bin/echo “hello world” >> /mydir/crontest.txt as a cron task: yep, cron is running for my user * * * * * cd current_path && rake my_task > /mydir/crontest2.txt 2>&1 : that gave me the error message “rake: command not found.”

“rake mytask” was working from the command-line, but not from cron; I’m guessing cron doesn’t load up the same env stuff that my user does. So, I set up the cron task like so: 1 * * * * bash /var/www/vhosts/myapp/shared/scripts/myscript.sh

and myscript.sh looks like so:

cd /app_curr_dir /usr/local/bin/rake mytask

And that works like a charm. So if you need to do some maintenance tasks on a schedule, that’s one nice way to handle it.

One Comment

  1. Raphaƫl wrote:

    You can just put :

    cd /myapp/path && RAILS_ENV=production /usr/local/bin/rake my:task

    directly in your crontab ;-)

    Sunday, March 15, 2009 at 9:58 am | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*