Results 1 to 6 of 6
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 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.
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!"
- 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
If you are looking at replicating changes across machines, have a look at incron or lsyncd instead.Should you be sitting wondering,
Which Batman is the best,
There's only one true answer my friend,
It's Adam Bloody West!
The Fifth Continent
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.
- 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