Search code examples
bashinotifyinotifywait

Using inotifywait (or alternative) to wait for rsync transfer to complete before running script?


I would like to setup inotifywait so that it monitors a folder and when something is copied to this folder (lsyncd which uses rsync) I would like inotifywait to sit tight and wait until rsync is done before calling a script to process the new folder.

I have been researching online to see if someone is doing this but I am not finding much.

I'm not the most well versed with bash scripting though I understand some basics.

Here is a little script I found that pauses for a second but it still triggers a dozen events per transfer:

 EVENTS="CLOSE_WRITE,MOVED_TO"

if [ -z "$1" ]; then
    echo "Usage: $0 cmd ..."
    exit -1;
fi

inotifywait -e "$EVENTS" -m -r --format '%:e %f' . | (
    WAITING="";
    while true; do
        LINE="";
        read -t 1 LINE;
        if test -z "$LINE"; then
            if test ! -z "$WAITING"; then
                    echo "CHANGE";
                    WAITING="";
            fi;
        else
            WAITING=1;
        fi;
    done) | (
    while true; do
        read TMP;
        echo $@
        $@
    done
)

I would be happy to provide more details or information.

Thank you.


Solution

  • Depending on what action you want to take, you may want to take a look at the tools provided by Watchman.

    There are two that might be most useful to you:

    1. If you want to initiate some action after the files are synced up, you may want to try using watchman-make. This is most appropriate if the action is to run a tool like make where the tool itself will look over the tree and produce its output (in other words: where you don't need to pass the precise list of changed files directly to your tool). You can have it run some other tool instead of make. There is a --settle option that you can use to have it wait a few moments after the latest file change notification before executing your tool.

      watchman-make --make='process-folder.sh' -p '**/*.*'

    2. watchman-wait is more closely related to inotifywait. It also waits for changes to settle before reporting files as changed, but because this tool doesn't coalesce multiple different file changes into a single event, the settle period is configured as a property of the tree being watched rather than as a command line parameter

    Disclaimer: I'm the creator of Watchman