Rake task from crontab?

Is there a good way to run a rake task from a crontab?

It seems like I’d need to put in my crontab:
“cd rails_dir; rake whatever”.

Which is kind of ugly. Is there some way to set a value in the rake
command line to eliminate the need for the explicit ‘cd’? Maybe
something involving the ability to set env variables on the command
line? Some env variable I could set that would tell rake what rails app
to run in, and allow the command to be executed without the working
directory actually being that dir?

Or maybe I should use script/runner instead? Any good easy way to get
script/runner to invoke a rake task?

Any advice from someone who’s accomplished this kind of thing before?

Jonathan

Jonathan R. wrote:

Is there a good way to run a rake task from a crontab?

Which is kind of ugly. Is there some way to set a value in the rake
command line to eliminate the need for the explicit ‘cd’?

I don’t know if something like “Make -C” exists with Rake

It seems like I’d need to put in my crontab:
“cd rails_dir; rake whatever”.

Seems to be the best way, that’s not soo ugly.

http://www.railsenvy.com/2007/6/11/ruby-on-rails-rake-tutorial

0 0 * * * cd /var/www/apps/rails_app/ && /usr/local/bin/rake
RAILS_ENV=production namespace:task

You can put this command in a little script shell named rake.sh

something like

#!/bin/sh
cd /var/www/apps/rails_app/ && /usr/local/bin/rake RAILS_ENV=production
$1

and in your crontab :
0 0 * * * ~/take.sh namespace:task

Litle bit more sexy :slight_smile:

I hop I helped you.

s/take.sh/rake.sh

:slight_smile: