# Startup script

**URL:** <https://www.ruby-forum.com/t/startup-script/121423>\
**Category:** Rails\
**Created:** [November 27, 2007, 9:19pm UTC](https://www.ruby-forum.com/t/startup-script/121423 "2007-11-27T21:19:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![monarailguy42](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9a28c/32.png) [@monarailguy42](https://www.ruby-forum.com/u/monarailguy42)\
**Post date:** [November 27, 2007, 9:19pm UTC](https://www.ruby-forum.com/t/startup-script/121423/1 "2007-11-27T21:19:51Z")

</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:** ![monarailguy42](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9a28c/32.png) [@monarailguy42](https://www.ruby-forum.com/u/monarailguy42)\
**Post date:** [November 27, 2007, 9:28pm UTC](https://www.ruby-forum.com/t/startup-script/121423/2 "2007-11-27T21:28:38Z")

</div>

On Nov 27, 2007 2:19 PM, monarailguy42 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> good solution. I am doing all of this on Windows so I can’t use  
> things like BackgroundDB. Thanks.

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

script/runner -e production “Model.method” &

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

---

<div class="post-metadata">

**Author:** ![monarailguy42](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9a28c/32.png) [@monarailguy42](https://www.ruby-forum.com/u/monarailguy42)\
**Post date:** [November 27, 2007, 9:58pm UTC](https://www.ruby-forum.com/t/startup-script/121423/3 "2007-11-27T21:58:00Z")

</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:** ![monarailguy42](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9a28c/32.png) [@monarailguy42](https://www.ruby-forum.com/u/monarailguy42)\
**Post date:** [November 27, 2007, 10:07pm UTC](https://www.ruby-forum.com/t/startup-script/121423/4 "2007-11-27T22:07:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![monarailguy42](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9a28c/32.png) [@monarailguy42](https://www.ruby-forum.com/u/monarailguy42)\
**Post date:** [November 27, 2007, 10:22pm UTC](https://www.ruby-forum.com/t/startup-script/121423/5 "2007-11-27T22:22:43Z")

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

---

<div class="post-metadata">

**Author:** ![monarailguy42](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9a28c/32.png) [@monarailguy42](https://www.ruby-forum.com/u/monarailguy42)\
**Post date:** [November 27, 2007, 10:08pm UTC](https://www.ruby-forum.com/t/startup-script/121423/6 "2007-11-27T22:08:03Z")

</div>

On Nov 27, 2007 2:57 PM, monarailguy42 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

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  
scriptpid=`cat $pidfile`  
if `kill -CHLD $scriptpid >/dev/null 2>&1`  
then  
exit 0  
fi  
echo “”  
echo “Stale $pidfile file, erasing…”  
rm -f $pidfile  
fi

echo “”  
echo “Couldn’t find script ‘$script’ running, running now.”  
echo “”

./$script

exit 0

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