How can I interrupt or abandon the inotifywait loop?

I have a little script that tracks files for changes withinotifywait . When something changes, a package of files is sent through a process (compiled, compressed, reorganized, etc.), which takes about ten seconds to complete.

Consider the following example:

touch oli-test
inotifywait -mq oli-test | while read EV; do sleep 5; echo "$EV"; done

If you run touch oli-testseveral times in another terminal, you will see that each cycle ends before it moves. This scenario is very real to me. If I forget to save the file during its processing or notice an error, the events add up and I wait a minute.

It seems to me that there are two methods that will make this workflow objectively better. I'm not sure which is easier or better, so I present both:

  • Abort previous runs and restart immediately. Currently, the scripting process is only a built-in set of commands. I could break them down into Bash functions, I'm not alone to break them further than that.

  • Discard the list of pending events so that if five events occur immediately (or while they are already being processed), it will be triggered again.

(Or both ... because I'm sure there are times when both will be useful)

I am also open to approaches other than inotifywait, but they should give me the same result and work on Ubuntu.

+6
source share
2 answers

, , , inotifywait :

inotifywait -mq oli-test | while read EV; do
    jobs -p | xargs kill -9
    (
        # do expensive things here
        sleep 5  # a placeholder for compiling
        echo "$EV"
    ) &
done
+4

, , bc (), bash .

lastRunTime=$(date +'%H%M%S.%N')

inotifywait -mr ./web -e create -e delete -e modify | while read file event tm; do

    currentTime=$(date +'%H%M%S.%N')
    delta=$(bc <<< "$lastRunTime - $currentTime")
    echo "$currentTime, $lastRunTime, $delta"

    if (( $(echo "$delta < -1.0" | bc -l) )); then
        echo "run"
        lastRunTime=$(date +'%H%M%S.%N')
    fi

done

: NOW, , <delta ( -1.0).

:

Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.
120845.009293691, 120842.581019388, -2.428274303
run
120845.018643243, 120845.017585539, -.001057704
120845.026360234, 120845.017585539, -.008774695

0

All Articles