4

Im running the following script to monitor the creation of new files, but its not quick enough. If two files get added at the same time, then this script only actions the first one.

Is there any way to tweak this to run faster or different?

#!/bin/sh

WATCHED_DIR=/var/www/magento/media/catalog/product
UPLOAD_DIR=/cloudfiles/magento/media/catalog/product

while :
do
  echo 'Watching directory: '$WATCHED_DIR 'for new files'
  while file=$(inotifywait -q -e create -r "$WATCHED_DIR" --format "%w%f")
  do
    loc=$file
    rem=$UPLOAD_DIR${file#$WATCHED_DIR}

    rsync --ignore-existing --inplace -q $loc $rem &
  done
done
Christian
  • 143

1 Answers1

2

I suggest to use the -m, --monitor option to inotifywait, in the following way:

#!/bin/sh                               

WATCHED_DIR="/var/www/magento/media/catalog/product"
UPLOAD_DIR="/cloudfiles/magento/media/catalog/product"    

echo "Watching directory: $WATCHED_DIR for new files"
inotifywait -m -q -e create -r "$WATCHED_DIR" --format "%w%f" |
  while read file
  do
    loc="$file"
    rem="$UPLOAD_DIR${file#$WATCHED_DIR}"

    rsync --ignore-existing --inplace -q "$loc" "$rem" &
  done

I also added some quoting to variables, to take into account the possibility of filenames with spaces.

enzotib
  • 96,093