# Scheduled tasks in Rails: Cron + wget = Best solution?

**URL:** <https://www.ruby-forum.com/t/scheduled-tasks-in-rails-cron-wget-best-solution/189615>\
**Category:** Rails\
**Created:** [June 4, 2010, 12:12am UTC](https://www.ruby-forum.com/t/scheduled-tasks-in-rails-cron-wget-best-solution/189615 "2010-06-04T00:12:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [June 4, 2010, 12:12am UTC](https://www.ruby-forum.com/t/scheduled-tasks-in-rails-cron-wget-best-solution/189615/1 "2010-06-04T00:12:27Z")

</div>

Hi there,

do you agree that for having scheduled tasks in Rails, the leanest  
solution is the following?

- Create a controller with an action for each task
- Implement the logic of the task as controller code
- Set up a cron job at the OS level that uses wget to invoke the URL of  
this controller/action at the appropriate time intervals

Advantages:

1. full access to all your Rails objects just as in a normal controller
2. you can develop and test just as you do normal actions
3. you can also invoke your tasks adhoc from a simple web page
4. You don’t consume any more memory by firing up additional ruby/rails  
processes

My additional question is: How would the cron job entry have to look  
like in order to let cron “log in” (as admin for example) before calling  
the action (for obvious security reasons)? This is what I have until  
now:

20 \* \* \* \* /usr/bin/wget --quiet -O -  
‘[http://www.mydomain.com/my\_controller/my\_action](http://www.mydomain.com/my_controller/my_action)’

Thank you for opinion and/or hints!  
Tom

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [June 4, 2010, 2:50am UTC](https://www.ruby-forum.com/t/scheduled-tasks-in-rails-cron-wget-best-solution/189615/2 "2010-06-04T02:50:00Z")

</div>

On Thu, Jun 3, 2010 at 5:12 PM, Tom Ha [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 20 \* \* \* \* /usr/bin/wget --quiet -O -  
> ‘[http://www.mydomain.com/my\_controller/my\_action](http://www.mydomain.com/my_controller/my_action)’

Use script/runner.

–  
Greg D.  
[destiney.com](http://destiney.com) | [gregdonald.com](http://gregdonald.com)

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [June 4, 2010, 5:02am UTC](https://www.ruby-forum.com/t/scheduled-tasks-in-rails-cron-wget-best-solution/189615/3 "2010-06-04T05:02:53Z")

</div>

Quoting Tom Ha [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):  
[snip]

> My additional question is: How would the cron job entry have to look  
> like in order to let cron “log in” (as admin for example) before calling  
> the action (for obvious security reasons)? This is what I have until  
> now:

One solution is to use HTTP Basic Authentication and curl (wget may be  
able to  
do this, I just know abt curl). Also, check for local\_request? in the  
controller (i.e., request is coming from the same computer).

HTH,  
Jeffrey

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [June 4, 2010, 3:26am UTC](https://www.ruby-forum.com/t/scheduled-tasks-in-rails-cron-wget-best-solution/189615/4 "2010-06-04T03:26:54Z")

</div>

Tom,  
I think every scheduled task I’ve seen has called methods on models,  
not controller actions. Here are some ways I’ve seen it done:

1. Sometimes, for cron jobs run in production, folks will do a  
“{#RAILS\_ROOT}/script/runner -e production ‘Model.method’ \>\> /dev/null  
2\>&1”
2. There’s repeated\_job by ddollar:  
[GitHub - ddollar/repeated\_job: Use Delayed::Job to run cron-like tasks](http://github.com/ddollar/repeated_job).  
It uses delayed\_job, which is pretty sweet.
3. Adam W. wrote a recent blog post about the shortcomings of  
cron: [http://adam.heroku.com/past/2010/4/13/rethinking\_cron/](http://adam.heroku.com/past/2010/4/13/rethinking_cron/). He  
suggests a combination of rufus-scheduler and Minion+RabbitMQ.

Good luck.

Tilde E.

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [June 4, 2010, 11:06am UTC](https://www.ruby-forum.com/t/scheduled-tasks-in-rails-cron-wget-best-solution/189615/5 "2010-06-04T11:06:21Z")

</div>

Hi Tom,

I am using BackgrounDRb and it does almost all of requirements you’ve  
mentioned.  
Check : [http://backgroundrb.rubyforge.org/](http://backgroundrb.rubyforge.org/)

Hope this helps!

Mutesa

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [June 4, 2010, 7:30am UTC](https://www.ruby-forum.com/t/scheduled-tasks-in-rails-cron-wget-best-solution/189615/6 "2010-06-04T07:30:57Z")

</div>

I’m using [GitHub - jmettraux/rufus-scheduler: scheduler for Ruby (at, in, cron and every jobs)](http://github.com/jmettraux/rufus-scheduler) to schedule mail  
sending. I’ve added scheduling code in config/initializers dir and  
it’s working quite well.

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [June 4, 2010, 5:17pm UTC](https://www.ruby-forum.com/t/scheduled-tasks-in-rails-cron-wget-best-solution/189615/7 "2010-06-04T17:17:00Z")

</div>

Thanks for all your suggestions!

(I like rufus-scheduler pretty much -\> small memory foot print…)

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [June 4, 2010, 11:21am UTC](https://www.ruby-forum.com/t/scheduled-tasks-in-rails-cron-wget-best-solution/189615/8 "2010-06-04T11:21:47Z")

</div>

Mutesa wrote:

> Hi Tom,
> 
> I am using BackgrounDRb and it does almost all of requirements you’ve  
> mentioned.  
> Check : [http://backgroundrb.rubyforge.org/](http://backgroundrb.rubyforge.org/)
> 
> Hope this helps!
> 
> Mutesa

Wheneverize gem
