Startup script

12 views
Skip to first unread message

monarailguy42

unread,
Nov 27, 2007, 3:19:13 PM11/27/07
to Ruby on Rails: Talk
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.

Greg Donald

unread,
Nov 27, 2007, 3:27:51 PM11/27/07
to rubyonra...@googlegroups.com

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

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


--
Greg Donald
http://destiney.com/

monarailguy42

unread,
Nov 27, 2007, 3:57:34 PM11/27/07
to Ruby on Rails: Talk
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.

On Nov 27, 2:27 pm, "Greg Donald" <gdon...@gmail.com> wrote:

Brian Hogan

unread,
Nov 27, 2007, 4:06:38 PM11/27/07
to rubyonra...@googlegroups.com
Can you explain what you're trying to do in more detail? There may be a better approach. 

Greg Donald

unread,
Nov 27, 2007, 4:07:36 PM11/27/07
to rubyonra...@googlegroups.com
On Nov 27, 2007 2:57 PM, monarailguy42 <Larry.E...@gmail.com> 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

monarailguy42

unread,
Nov 27, 2007, 4:22:27 PM11/27/07
to Ruby on Rails: Talk
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.

On Nov 27, 3:06 pm, "Brian Hogan" <bpho...@gmail.com> wrote:
> Can you explain what you're trying to do in more detail? There may be a
> better approach.
>
Reply all
Reply to author
Forward
0 new messages