They will be run one after another unless you background the rsync
with a &
at the end.
This will run them concurrently which is not what you want:
inotifywait -mrq -e close_write -e create -e delete -e move /var/www | while read file
do
rsync -av /var/www1/ /var/www2/ &
done
You probably also don't want it run ten times consecutively. This may be what you're after:
while inotifywait -rq -e close_write -e create -e delete -e move /var/www
do
rsync -av /var/www1/ /var/www2/
done
Also, be aware that running inotifywait
recursively on a large /var/www
may take a while to set up on each invocation. You may want to limit its scope to only watching active subdirectories or just use cron
to periodically run rsync
.
Edit:
Try this demo:
$ mkdir inotifytest
$ cd inotifytest
$ echo something > testfile
$ while inotifywait -rq -e access .; do echo "processing"; sleep 3; done # test 1
Now in another terminal, do this:
$ cd inotifytest
$ for i in {1..10}; do cat testfile > /dev/null; done
In the first terminal you should see "processing" one time. If you do the for
/cat
loop (which represents files being added or deleted in your directories) again, you'll see it again.
Now in the first terminal, interrupt the loop with Ctrl-C and start this loop:
$ inotifywait -mrq -e access .| while read file; do echo "processing"; sleep 3; done # test 2
In the second terminal, run the for
/cat
loop again. This time in the first terminal you'll see "processing" ten times with a three second pause between each one (representing the time it takes rsync
to complete. Now if you were to run the for
loop several times in rapid succession you might be waiting all day.
I don't think a command or shell builtin for this exists, as it's a trivial subset of what the Bourne shell for
loop is designed for and implementing a command like this yourself is therefore quite simple.
Per JimB's suggestion, use the Bash builtin for generating sequences:
for i in {1..10}; do command; done
For very old versions of bash, you can use the seq
command:
for i in `seq 10`; do command; done
This iterates ten times executing command
each time - it can be a pipe or a series of commands separated by ;
or &&
. You can use the $i
variable to know which iteration you're in.
If you consider this one-liner a script and so for some unspecified (but perhaps valid) reason undesireable you can implement it as a command, perhaps something like this on your .bashrc (untested):
#function run
run() {
number=$1
shift
for i in `seq $number`; do
$@
done
}
Usage:
run 10 command
Example:
run 5 echo 'Hello World!'
Best Answer
....but you've not shown us any of the code which you used.
Did you check if the process you started was still running? What logfile? The code you've shown doesn't write any log files.
It would have helped if you had (also) provided some details of which linux this relates to.
Here's a sys V init script which should do the trick (if you know how to deploy it)....