Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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.

share|improve this answer
    
excellent! shall give it a try :) –  Christian Feb 9 '12 at 14:39
    
That seems to have done the trick. Thank you VERY much enzotib! –  Christian Feb 9 '12 at 19:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.