# Continously running a script

**URL:** <https://www.ruby-forum.com/t/continously-running-a-script/187306>\
**Category:** Rails\
**Created:** [April 21, 2010, 2:17pm UTC](https://www.ruby-forum.com/t/continously-running-a-script/187306 "2010-04-21T14:17:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tashfeen.ekram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3ab097/32.png) [@tashfeen.ekram](https://www.ruby-forum.com/u/tashfeen.ekram)\
**Post date:** [April 21, 2010, 2:17pm UTC](https://www.ruby-forum.com/t/continously-running-a-script/187306/1 "2010-04-21T14:17:00Z")

</div>

I am writing a rails script that I need to run continuosly. It  
basiclly scans the a particular table in my DB and makes queries to an  
outside service and updates the table as needed. I would like to keep  
the script constatnly running run. If I write the script as a runner,  
is there a way to do this?

Thanks!

---

<div class="post-metadata">

**Author:** ![tashfeen.ekram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3ab097/32.png) [@tashfeen.ekram](https://www.ruby-forum.com/u/tashfeen.ekram)\
**Post date:** [April 21, 2010, 2:29pm UTC](https://www.ruby-forum.com/t/continously-running-a-script/187306/2 "2010-04-21T14:29:18Z")

</div>

By continuously do you mean ‘scan table, scan table, scan table …’ or  
‘scan table, wait for 10 minutes, repeat’

If it is the latter then you could simply set up a cron job to run every  
few  
minutes.

Here’s some details on how you might do it

> **[A rake task to install crons](http://peter-hickman.blogspot.com/2009/09/rake-task-to-install-crons.html)**
>
> Assuming the cron tasks you want to run are stored in script/crons/{daily,hourly,monthly,weekly}/... this rake task will create the necessa...

> **[Crons for Rails](http://peter-hickman.blogspot.com/2009/11/crons-for-rails.html)**
>
> Well all the stuff that has been building up in this blog has now turned into a plugin for Rails that automates the installation and setup o...

This sort of thing occurs a few times in my blog.

Hope it might be of help.

Of course this assumes that you are running on \*nix or OSX

---

<div class="post-metadata">

**Author:** ![tashfeen.ekram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3ab097/32.png) [@tashfeen.ekram](https://www.ruby-forum.com/u/tashfeen.ekram)\
**Post date:** [April 21, 2010, 4:18pm UTC](https://www.ruby-forum.com/t/continously-running-a-script/187306/3 "2010-04-21T16:18:47Z")

</div>

Actually, I am interested in the former. That is, scan table and once  
it is done start form the top again and scan again without any delay.

Thanks for the resource.

On Apr 21, 8:25 am, Peter H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![tashfeen.ekram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3ab097/32.png) [@tashfeen.ekram](https://www.ruby-forum.com/u/tashfeen.ekram)\
**Post date:** [April 21, 2010, 5:02pm UTC](https://www.ruby-forum.com/t/continously-running-a-script/187306/4 "2010-04-21T17:02:08Z")

</div>

On Apr 21, 7:17 am, “tashfeen.ekram” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Actually, I am interested in the former. That is, scan table and once  
> it is done start form the top again and scan again without any delay.

[http://cr.yp.to/daemontools.html](http://cr.yp.to/daemontools.html) should help (on Unix).

Your Ruby script has an endless loop. daemontools comes in to start it  
initially, or if the script fails, or for you to start/stop .

Stephan

---

<div class="post-metadata">

**Author:** ![tashfeen.ekram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3ab097/32.png) [@tashfeen.ekram](https://www.ruby-forum.com/u/tashfeen.ekram)\
**Post date:** [May 6, 2010, 8:15pm UTC](https://www.ruby-forum.com/t/continously-running-a-script/187306/5 "2010-05-06T20:15:40Z")

</div>

On 6 May 2010 18:51, tashfeen.ekram [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks!
> 
> I can think of a few ways to write a script with a continous loop.  
> Just wondering though if there is a straight forward way to do it.

while true  
Model.all.each do |record|  
#… stuff with record  
end  
end

Writing a continuous loop isn’t hard (I do it myself by mistake all  
the time!) - but monitoring it to ensure it’s running is the problem.

---

<div class="post-metadata">

**Author:** ![tashfeen.ekram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3ab097/32.png) [@tashfeen.ekram](https://www.ruby-forum.com/u/tashfeen.ekram)\
**Post date:** [May 6, 2010, 7:52pm UTC](https://www.ruby-forum.com/t/continously-running-a-script/187306/6 "2010-05-06T19:52:51Z")

</div>

Thanks!

I can think of a few ways to write a script with a continous loop.  
Just wondering though if there is a straight forward way to do it.

The one way I can think of is to have a integer that is the index of  
the record in the DB and then just reset it when it hits the number of  
records in the DB.
