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.
Add a method (that includes your 2 second sleep) to your model, then run it like this:
script/runner -e production "Model.method" &
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.
Can you explain what you’re trying to do in more detail? There may be a better approach.
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
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.