Search code examples
bashgitshinotifyinotifywait

Why does inotifywait stop working consistently after a git checkout on the files it's watching?


I am running inotifywait on a directory of files:

inotifywait --quiet --monitor  \
  --event modify \
  --event moved_to \
  --event moved_from \
  --event move \
  --event unmount \
  --event create \
  --format '%w' /path/to/*.json | \
while read FILE; do
  echo "$FILE was changed."
done

This works well until I run git checkout [some existing branch name]. After that, inotifywait stops responding to files that git updated when checking out the branch (That is, is stops responding to files that are different between the two branches).

Why does inotifywait stop responding to these files? Is there a way for it to re-see those files?


Solution

  • inotifywait --event create /path/to/*.json doesn't work at all, because an already-existing file can't have a create event, and /path/to/*.json is replaced with a list of already-existing filenames by your shell before inotifywait is even started; any file that doesn't exist won't be included, and when inotifywait open()s the existing files, it'll get only the preexisting copies, and thus won't even see any new files created under the same name in the future.

    Follow the directory instead:

    inotifywait --quiet --monitor  \
      --event modify \
      --event moved_to \
      --event moved_from \
      --event move \
      --event unmount \
      --event create \
      --format '%w' /path/to | \
    while IFS= read -r file; do
      [[ $file = *.json ]] || continue  # ignore any non-*.json files
      echo "$file was changed."
    done