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 #1
- Join Date
- Apr 2010
run a bash script in every two second
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.
- 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 #3
- Join Date
- May 2011
There is also inittab but I think it refreshes every 5 seconds. I think you're stuck with using a loop, e.g.:
#!/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.What do we want?
When do we want 'em?
Doesn't really matter does it!?
The Fifth Continent
- 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 fasterYou must always face the curtain with a bow.
- 06-29-2011 #6
- Join Date
- Jun 2011
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:
#!/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:
#!/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