Run a script automatically in every 2 sec.

Rick Stevens ricks at nerd.com
Wed Jun 29 16:22:38 UTC 2011


On 06/29/2011 12:08 AM, ShibuThomas wrote:
> Hi ricks,
> 
> I wrote a one shell script which contains code for synchronize files
> between two machines.I want to execute this script every 2 second.How
> can I do this?.When I gone through net found that usually cron job is
> used for doing same.But the minimum time frame in cron is 1 minitues.One
> method is to keep a loop inside my script and execute it.But I would
> like to know is there any other way to do the same.
> So how can execute my script every 2 second.Please provide some example
> code.

Good lord!  Every 2 seconds?  That's a bit extreme.

Yes, the minimum granularity for a cron job is 1 minute.  If you need
finer granularity, you need to run a script that does a sleep call:

	#!/bin/bash
	while /bin/true; do
	    rsync (or whatever) >/dev/null 2>&1
	    sleep 2
	done

This will do the rsync command (discarding any messages), wait for
two seconds, then repeat.

I really, REALLY recommend you do NOT do this as it can be a massive
resource hog.  If the two machines are on the same LAN, the best bet is
to share the target directory between them using NFS rather than doing
some sync job.

Alternately, look at the inotifywait stuff. You can put a watch on a
directory on the source machine and only launch the sync if a file is
written in it. You'd need to "yum install inotify-tools", and here's an
example script:

------------------------------- CUT HERE -----------------------------
#!/bin/bash
# Filename:	watchuploads.sh
# Author:	Rick Stevens, AllDigital, Inc.
# Last Edit:	29 March 2011
#
# Synopsis:
#	This script uses inotifywait to watch for newly uploaded files
#	in the /opt/uploads directory.  When something is uploaded, it
#	runs rsync to copy the file to another machine.
#		
# NOTES:
#	It's probably best to run this script in a screen(1) session or
#	via "nohup /usr/local/bin/watchuploads.sh >/dev/null 2>&1 &".
#

# Set up some variables to make life easier...
WATCHPT="/opt/uploads"			# Directory to watch for uploads

# And away we go...
while /bin/true; do			# Start infinite loop
    FNAME=`inotifywait -e close_write --format "%f" $WATCHPT
2>/dev/null`					# Watch for changes and grab
					# filename
    nohup rsync $WATCHPT/$FNAME remotemachine:$WATCHPT/ \
	 >>/dev/null 2>&1 &		# Launch rsync
done
------------------------------- CUT HERE -----------------------------

This is FAR more efficient than doing a sleep(1) and won't put as much
of a load on the system. Look at the man pages for inotifywait(1),
inotifywatch(1) and inotify(7).
----------------------------------------------------------------------
- Rick Stevens, Systems Engineer, C2 Hosting          ricks at nerd.com -
- AIM/Skype: therps2        ICQ: 22643734            Yahoo: origrps2 -
-                                                                    -
-       Charter Member of the International Sarcasm Society          -
-                "Yeah, like we need YOUR support!"                  -
----------------------------------------------------------------------




More information about the Redhat-install-list mailing list