# Startup script

**URL:** <https://discuss.rubyonrails.org/t/startup-script/20890>\
**Category:** rubyonrails-talk\
**Created:** [November 27, 2007, 8:19pm UTC](https://discuss.rubyonrails.org/t/startup-script/20890 "2007-11-27T20:19:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Larry\_Erickson](https://avatars.discourse-cdn.com/v4/letter/l/d78d45/32.png) [@Larry\_Erickson](https://discuss.rubyonrails.org/u/Larry_Erickson)\
**Post date:** [November 27, 2007, 8:19pm UTC](https://discuss.rubyonrails.org/t/startup-script/20890/1 "2007-11-27T20:19:13Z")

</div>

Hi, I need to run a ruby file when the server starts up and then at 2 second intervals as long as the server is running. What is the preferred way to do this in Rails? Can I put something in the environment.rb or something like that? Currently I made this script a controller with a while true loop that sleeps for 2 seconds before it executes (basically an infinite loop). I run the script by hitting the URL and then I close out my web browser because it never finishes. This is allowing me to test fine but is obviously not a good solution. I am doing all of this on Windows so I can't use things like BackgroundDB. Thanks.

---

<div class="post-metadata">

**Author:** ![Greg\_Donald1](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/greg_donald1/32/1313_2.png) [@Greg\_Donald1](https://discuss.rubyonrails.org/u/Greg_Donald1)\
**Post date:** [November 27, 2007, 8:27pm UTC](https://discuss.rubyonrails.org/t/startup-script/20890/2 "2007-11-27T20:27:51Z")

</div>

Add a method (that includes your 2 second sleep) to your model, then run it like this:

script/runner -e production "Model.method" &

---

<div class="post-metadata">

**Author:** ![Larry\_Erickson](https://avatars.discourse-cdn.com/v4/letter/l/d78d45/32.png) [@Larry\_Erickson](https://discuss.rubyonrails.org/u/Larry_Erickson)\
**Post date:** [November 27, 2007, 8:57pm UTC](https://discuss.rubyonrails.org/t/startup-script/20890/3 "2007-11-27T20:57:34Z")

</div>

Thanks for the help. Took me a while to get it to work because I forgot to self.methodname since it has to be a class method. Any ideas on how I can get this to start automatically.

---

<div class="post-metadata">

**Author:** ![bphogan](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/bphogan/32/4186_2.png) [@bphogan](https://discuss.rubyonrails.org/u/bphogan)\
**Post date:** [November 27, 2007, 9:06pm UTC](https://discuss.rubyonrails.org/t/startup-script/20890/4 "2007-11-27T21:06:38Z")

</div>

Can you explain what you’re trying to do in more detail? There may be a better approach.

---

<div class="post-metadata">

**Author:** ![Greg\_Donald1](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/greg_donald1/32/1313_2.png) [@Greg\_Donald1](https://discuss.rubyonrails.org/u/Greg_Donald1)\
**Post date:** [November 27, 2007, 9:07pm UTC](https://discuss.rubyonrails.org/t/startup-script/20890/5 "2007-11-27T21:07:36Z")

</div>

You can make it into a shell script and then put that into a cron job. The shell script will produce a pid you can use to check for to start/stop appropriately:

#!/bin/sh script="foo.sh" pidfile="foo.pid"

if test -r $pidfile then &nbsp;&nbsp;scriptpid=`cat $pidfile` &nbsp;&nbsp;if `kill -CHLD $scriptpid \>/dev/null 2\>&1` &nbsp;&nbsp;then &nbsp;&nbsp;&nbsp;&nbsp;exit 0 &nbsp;&nbsp;fi &nbsp;&nbsp;echo "" &nbsp;&nbsp;echo "Stale $pidfile file, erasing..." &nbsp;&nbsp;rm -f $pidfile fi

echo "" echo "Couldn't find script '$script' running, running now." echo ""

./$script

exit 0

---

<div class="post-metadata">

**Author:** ![Larry\_Erickson](https://avatars.discourse-cdn.com/v4/letter/l/d78d45/32.png) [@Larry\_Erickson](https://discuss.rubyonrails.org/u/Larry_Erickson)\
**Post date:** [November 27, 2007, 9:22pm UTC](https://discuss.rubyonrails.org/t/startup-script/20890/6 "2007-11-27T21:22:27Z")

</div>

Part of my application requires communication to a PLC. This PLC has a SOAP web service in which you can access the current points in the PLC. My application needs to poll this SOAP service every couple of seconds and update a database table with the results. Ideally I want this to automatically start when my web server starts up and keep going for the duration that the web server runs. Thanks.
