# Continously running a script

**URL:** <https://discuss.rubyonrails.org/t/continously-running-a-script/51080>\
**Category:** rubyonrails-talk\
**Created:** [April 21, 2010, 12:15pm UTC](https://discuss.rubyonrails.org/t/continously-running-a-script/51080 "2010-04-21T12:15:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tashfeen\_Ekram](https://avatars.discourse-cdn.com/v4/letter/t/bbe5ce/32.png) [@Tashfeen\_Ekram](https://discuss.rubyonrails.org/u/Tashfeen_Ekram)\
**Post date:** [April 21, 2010, 12:15pm UTC](https://discuss.rubyonrails.org/t/continously-running-a-script/51080/1 "2010-04-21T12:15:20Z")

</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:** ![Peter\_Hickman1](https://avatars.discourse-cdn.com/v4/letter/p/8e7dd6/32.png) [@Peter\_Hickman1](https://discuss.rubyonrails.org/u/Peter_Hickman1)\
**Post date:** [April 21, 2010, 12:25pm UTC](https://discuss.rubyonrails.org/t/continously-running-a-script/51080/2 "2010-04-21T12:25:31Z")

</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

[http://peter-hickman.blogspot.com/2009/09/rake-task-to-install-crons.html](http://peter-hickman.blogspot.com/2009/09/rake-task-to-install-crons.html)

[http://peter-hickman.blogspot.com/2009/11/crons-for-rails.html](http://peter-hickman.blogspot.com/2009/11/crons-for-rails.html)

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://avatars.discourse-cdn.com/v4/letter/t/bbe5ce/32.png) [@Tashfeen\_Ekram](https://discuss.rubyonrails.org/u/Tashfeen_Ekram)\
**Post date:** [April 21, 2010, 2:17pm UTC](https://discuss.rubyonrails.org/t/continously-running-a-script/51080/3 "2010-04-21T14:17:31Z")

</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.

---

<div class="post-metadata">

**Author:** ![Stephan\_Wehner](https://avatars.discourse-cdn.com/v4/letter/s/b19c9b/32.png) [@Stephan\_Wehner](https://discuss.rubyonrails.org/u/Stephan_Wehner)\
**Post date:** [April 21, 2010, 3:00pm UTC](https://discuss.rubyonrails.org/t/continously-running-a-script/51080/4 "2010-04-21T15:00:39Z")

</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.

[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://avatars.discourse-cdn.com/v4/letter/t/bbe5ce/32.png) [@Tashfeen\_Ekram](https://discuss.rubyonrails.org/u/Tashfeen_Ekram)\
**Post date:** [May 6, 2010, 5:51pm UTC](https://discuss.rubyonrails.org/t/continously-running-a-script/51080/5 "2010-05-06T17:51:18Z")

</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.

---

<div class="post-metadata">

**Author:** ![Michael\_Pavling](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Michael\_Pavling](https://discuss.rubyonrails.org/u/Michael_Pavling)\
**Post date:** [May 6, 2010, 6:13pm UTC](https://discuss.rubyonrails.org/t/continously-running-a-script/51080/6 "2010-05-06T18:13:40Z")

</div>

while true &nbsp;&nbsp;Model.all.each do |record| &nbsp;&nbsp;&nbsp;&nbsp;#... stuff with record &nbsp;&nbsp;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.
