# How to use Rails cron?

**URL:** <https://www.ruby-forum.com/t/how-to-use-rails-cron/105158>\
**Category:** Rails\
**Created:** [May 26, 2007, 11:02am UTC](https://www.ruby-forum.com/t/how-to-use-rails-cron/105158 "2007-05-26T11:02:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![selvaraj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8797f3/32.png) [@selvaraj](https://www.ruby-forum.com/u/selvaraj)\
**Post date:** [May 26, 2007, 11:02am UTC](https://www.ruby-forum.com/t/how-to-use-rails-cron/105158/1 "2007-05-26T11:02:18Z")

</div>

Hi,

How to use rails cron? can I get any link of reference guide?

---

<div class="post-metadata">

**Author:** ![selvaraj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8797f3/32.png) [@selvaraj](https://www.ruby-forum.com/u/selvaraj)\
**Post date:** [May 26, 2007, 3:28pm UTC](https://www.ruby-forum.com/t/how-to-use-rails-cron/105158/2 "2007-05-26T15:28:34Z")

</div>

hi

if u know any link pleae post here…

Selvaraj wrote:

> Hi,
> 
> How to use rails cron? can I get any link of reference guide?

---

<div class="post-metadata">

**Author:** ![selvaraj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8797f3/32.png) [@selvaraj](https://www.ruby-forum.com/u/selvaraj)\
**Post date:** [May 27, 2007, 4:08pm UTC](https://www.ruby-forum.com/t/how-to-use-rails-cron/105158/3 "2007-05-27T16:08:22Z")

</div>

Hi Selvaraj,

I’m assuming you mean tying cron jobs to rails method calls?  
Rails has a built in script called runner that you can use to call  
Model methods, and there are a couple of other options as well  
depending what action you want to run with the cron job.  
Have a look here for a bit of a summary:  
[http://wiki.rubyonrails.org/rails/pages/HowToRunBackgroundJobsInRails](http://wiki.rubyonrails.org/rails/pages/HowToRunBackgroundJobsInRails)

So the answer in short is it depends a bit what you want to do, for  
instance I had a case where I needed to run a cron job to check the  
expiry of an rss feed related model so I used script/runner  
path\_to\_rails\_app/script/runner -e production “Channel.check\_expiry”  
with a cron job to check this periodically.

This approach I believe is aimed at when you need to run Model methods  
only, to run controller actions you may want to use something like  
BackgrounDrb ([http://backgroundrb.rubyforge.org/](http://backgroundrb.rubyforge.org/)) and setup a method  
to perform whatever action you need to run in the background.

Hope this helps,

Paul

---

<div class="post-metadata">

**Author:** ![selvaraj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8797f3/32.png) [@selvaraj](https://www.ruby-forum.com/u/selvaraj)\
**Post date:** [May 28, 2007, 6:09am UTC](https://www.ruby-forum.com/t/how-to-use-rails-cron/105158/4 "2007-05-28T06:09:19Z")

</div>

Hi Paul

I too need to run cron jobs to clean up some tables periodically.  
The context is that an invite sent from one user – a member – to a  
prospective member needs to be expired after a period of time.

This could give rise to race conditions because the record may ‘get  
expired’ exactly at the time it is being edited by a user (and therefore  
by the foreground rails controller)

So how does one take care of these situations in rails?

Thanks

pcrawfor wrote:

> Hi Selvaraj,
> 
> I’m assuming you mean tying cron jobs to rails method calls?  
> Rails has a built in script called runner that you can use to call  
> Model methods, and there are a couple of other options as well  
> depending what action you want to run with the cron job.  
> Have a look here for a bit of a summary:  
> [http://wiki.rubyonrails.org/rails/pages/HowToRunBackgroundJobsInRails](http://wiki.rubyonrails.org/rails/pages/HowToRunBackgroundJobsInRails)
> 
> So the answer in short is it depends a bit what you want to do, for  
> instance I had a case where I needed to run a cron job to check the  
> expiry of an rss feed related model so I used script/runner  
> path\_to\_rails\_app/script/runner -e production “Channel.check\_expiry”  
> with a cron job to check this periodically.
> 
> This approach I believe is aimed at when you need to run Model methods  
> only, to run controller actions you may want to use something like  
> BackgrounDrb ([http://backgroundrb.rubyforge.org/](http://backgroundrb.rubyforge.org/)) and setup a method  
> to perform whatever action you need to run in the background.
> 
> Hope this helps,
> 
> Paul

---

<div class="post-metadata">

**Author:** ![selvaraj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8797f3/32.png) [@selvaraj](https://www.ruby-forum.com/u/selvaraj)\
**Post date:** [November 9, 2009, 6:27am UTC](https://www.ruby-forum.com/t/how-to-use-rails-cron/105158/5 "2009-11-09T06:27:52Z")

</div>

Hi Selvaraj

> hi
> 
> if u know any link pleae post here…
> 
> Selvaraj wrote:
> 
> > Hi,
> > 
> > How to use rails cron? can I get any link of reference guide?

[http://railsenvy.com/2007/6/11/ruby-on-rails-rake-tutorial](http://railsenvy.com/2007/6/11/ruby-on-rails-rake-tutorial)

Sijo

---

<div class="post-metadata">

**Author:** ![selvaraj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8797f3/32.png) [@selvaraj](https://www.ruby-forum.com/u/selvaraj)\
**Post date:** [November 7, 2009, 2:07pm UTC](https://www.ruby-forum.com/t/how-to-use-rails-cron/105158/6 "2009-11-07T14:07:42Z")

</div>

hi all…  
I m using cron for my application.  
already for my 1st task i use the schedule.rb  
again i want to use cron job again for my 2nd task.  
is it possible to use the same schedule.rb file?  
why i ask is whenever i modify any of my tasks settings it overwrites  
the schedule.rb file.  
so how can i solve this…  
any ideas.  
Thanks for Any HELPS
