1

Currently inotifywait is watching git server folders. End it emits only when specific file modified. The problem is, when changes are pushed to git server, inotifywait triggers few times. I don’t know why.

So how can I do the next: prevent inotifywait from making duplicates?

I was thinking about the algorithm: when triggered first time->sleep script so it won’t scan next changes for 5 seconds->resume script. But it sounds idiotic... Can you help me achieving this? Thanks!!

jww
  • 97,681
  • 90
  • 411
  • 885
Andrew Gorpenko
  • 187
  • 1
  • 1
  • 10
  • 1
    You should probably show the relevant part of your code. Maybe related: [inotify event IN_MODIFY occurring twice for tftp put](https://stackoverflow.com/q/32377517/608639). According to the accepted answer, you should probably use `IN_CLOSE_WRITE`. Also see [Why do inotify events fire more than once](https://askubuntu.com/q/710422) on Ubuntu.SE. – jww Jul 23 '17 at 17:31
  • Which exactly file do you track? It could be that it changed not once. – max630 Jul 24 '17 at 06:10

2 Answers2

1

As I mentioned in your other question, you can setup first a post-receive hook which would checkout the repo for you whenever there is a push done to the Git server.

Not only can you test your inotify function when monitoring those files changed on checkout, but you could even consider not using inotify at all, and using the hook to trigger your notification.
A post-receive hook can list files, and you can then trigger your notification only for certain files.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

Here is how I solved it for my own needs. I am monitoring a path so that it will auto-compile for syntax errors. And I was bothered with the duplicate events emitted by inotifywait. Add the syncheck function into your .bashrc:

syncheck() {
  declare -A fileMap
  inotifywait --exclude .swp --format '%w%f' -e modify,create -m -r -q $* | \
  while read FILE; do
    es=$(date +'%s')
    case "$FILE" in
    *.hs)
    if [[ ${fileMap[$FILE]} != $es ]];then
        sdiff=$((es-${fileMap[$FILE]:-0}))
        fileMap[$FILE]=$es
        ((sdiff < 3)) && continue
        ghc -fno-code $FILE
        echo "---------------------------------------------"
    fi
    ;;
    esac
  done
}

How to use:

cd ~/src/proj/
. ~/.bashrc
syncheck .

In another terminal, modify or create a Haskell file in ~/src/proj/ location. The syncheck while-loop will detect this and auto-compile for syntax errors.

The key idea to this duplicate events suppression solution is Unix`s epoch seconds and bash dictionary.

daparic
  • 3,794
  • 2
  • 36
  • 38