Results 1 to 6 of 6
Hi all
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 06-29-2011 #1Just Joined!
- Join Date
- Apr 2010
- Posts
- 16
run a bash script in every two second
Hi all
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.
Regards,
ShibuThomas
- 06-29-2011 #2
Use the sleep command. Make a separate loop script that sleeps for two seconds and then launches the synchroniser script.
"I'm just a little old lady; don't try to dazzle me with jargon!"
- 06-29-2011 #3Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,700
There is also inittab but I think it refreshes every 5 seconds. I think you're stuck with using a loop, e.g.:
When I need to do this type of thing, I usually do it in Perl, and I detach from the terminal and fork the process to the background and make sure another copy of the process is not already running.Code:#!/bin/sh while :; do echo blah sleep 2 done
- 06-29-2011 #4
If you are looking at replicating changes across machines, have a look at incron or lsyncd instead.
If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)
My new blog. It's probably not as good as I think it is.
The Fifth Continent reborn
- 06-29-2011 #5
I would recoomend *not* to (r?)sync every 2 seconds.
This will very likely lead to race conditions, unless proper locking is used.
Also it is ressource heavy to traverse a directory structure every 2s and build a filelist. Probably most of the time for no actual filecopy at all.
The solution to your problem maybe is lsyncd - Lsyncd (Live Syncing Daemon) synchronizes local directories with a remote targets - Google Project Hosting
It is a inotify based synchronistation tool, so it will only take actions, if actually some files were changed.
Also, it features delayed synchronisation: It can be configured to wait a bit after the first file change before it syncs.
The logic behind this is to first gather a bunch of changes and sync them at once, instead of mutliple syncs.
@elija: I need to type faster
You must always face the curtain with a bow.
- 06-29-2011 #6Just Joined!
- Join Date
- Jun 2011
- Posts
- 1
Hi Shibupthomas,
A simple method for running a script every two seconds is to use the watch command. By default it runs every two seconds but you can change that with the -n switch. Here's an example:
The watch command uses wpa_cli to scan for wireless access points and then prints the results every second.Code:#!/bin/sh watch -n 1 'wpa_cli scan && wpa_cli scan_r'
Make this script executable and run it as root. If you get an error message upon running this script, then try this one:
Feel free to email me if you have any questions:Code:#!/bin/sh wpa_supplicant -B -D wext -i eth1 -C /var/run/wpa_supplicant & #Replace wext with the driver your wireless card uses(wext should work for most) and replace eth1 with your wireless interface name watch -n 1 'wpa_cli scan && wpa_cli scan_r' && #Monitor wireless access points in "real time" killall wpa_supplicant #This will restart wpa_supplicant just in case your network management program doesn't play nice
losstfeedback@gmail.com


Reply With Quote
