2

I want to run service to listen on file modifying and for every add to file delete it from file and append to another file I tried this code but it is not working it like going in infinite loop

   inotifywait -m -e modify "$1" |
    while read folder eventlist eventfile
    do
        cat "$1">>$DESTINATION_FILE
        >$1
    done 
anishsane
  • 20,270
  • 5
  • 40
  • 73
Steve
  • 503
  • 3
  • 9
  • 19

2 Answers2

2

Each time you truncate the file, that registers as a modification, which triggers another truncation, etc. Try testing if the file contains anything in the body of the loop.

inotifywait -m -e modify "$1" |
while read folder eventlist eventfile
do
    # Only copy-and-clear if the file is not empty
    if [ -s "$1" ]; then
        cat "$1" >> "$DESTINATION_FILE"
        # What if the file is modified here?
        >$1
    fi
done

See my comment between cat and the truncation. You would never put those modifications in $DESTINATION_FILE, because you would erase them before the next iteration of the loop. This isn't really avoidable, unless your operating system allows you to obtain a lock on $1 prior to the cat, then release the lock after the truncation, so that only one process can write to the file at a time.

anishsane
  • 20,270
  • 5
  • 40
  • 73
chepner
  • 497,756
  • 71
  • 530
  • 681
2

As pointed out by chepner, the reverting of the changes will also be treated as file modify.

A way out is:

  1. remove -m parameter
  2. Manually implement a while loop in bash

e.g.

cp "$1" "$1.bak"
while true; do inotifywait -e modify "$1" | {
    read folder eventlist eventfile;
    cat "$1" >> "$DESTINATION_FILE";
    # OR
    # diff  "$1" "$1.bak"  >> "$DESTINATION_FILE";
    cp "$1.bak" "$1";
    }
done

Note: I haven't tested above code myself.

Note2: There may be atomicity issues. There are times when the file modifications are not being monitored. Hence, when this cat > operation or cp operations are in progress, someone may attempt to write to "$1" file, which will be missed.

anishsane
  • 20,270
  • 5
  • 40
  • 73