Building web applications using experience-driven strategies, agile development practices and open source technology.

What to do when cron isn't fast enough

Sometimes you need to automate a task that needs to happen every few seconds rather than minutes or hours. Recently I needed to rsync two directories between servers every few seconds. Normally you would just make a cron job and call it a day, but the requirement of every 3 seconds makes cron inadequate. Any easy solution is to write a script in a loop and make an init.d script to start it or included it in your /etc/rc.local file. Below is a simple example...


#!/bin/bash
while true
do
rsync -azqvr --force /my/dir/ user@host:/my/dir/
sleep 3
done

All thats happening here is we start an endless loop, then use sleep to control the interval in seconds your cmd executes. Pretty simple eh. Post a comment if you know a better way. Thanks!

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options