# Rake task from crontab?

**URL:** <https://www.ruby-forum.com/t/rake-task-from-crontab/114520>\
**Category:** Rails\
**Created:** [September 13, 2007, 8:22pm UTC](https://www.ruby-forum.com/t/rake-task-from-crontab/114520 "2007-09-13T20:22:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [September 13, 2007, 8:22pm UTC](https://www.ruby-forum.com/t/rake-task-from-crontab/114520/1 "2007-09-13T20:22:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [September 21, 2007, 4:15pm UTC](https://www.ruby-forum.com/t/rake-task-from-crontab/114520/2 "2007-09-21T16:15:38Z")

</div>

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](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 🙂

I hop I helped you.

---

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [September 21, 2007, 4:16pm UTC](https://www.ruby-forum.com/t/rake-task-from-crontab/114520/3 "2007-09-21T16:16:30Z")

</div>

s/take.sh/rake.sh

🙂
