Skip to content

Instantly share code, notes, and snippets.

@gwpl
Created May 16, 2017 23:01
Show Gist options
  • Save gwpl/fd1420d73480329d4e8e4e332d04535c to your computer and use it in GitHub Desktop.
Save gwpl/fd1420d73480329d4e8e4e332d04535c to your computer and use it in GitHub Desktop.
inotifywait to exec specified command on each file change -> e.g. run "make", "pdflatex", or any other compile/upload or other command of choice! (Thanks https://superuser.com/a/181543/81861 ! )
#!/bin/bash
# Example usecase:
# inotify_exec_on_file_change.sh /path/phd paper.tex pdflatex paper.tex
dirn="$1"
filen="$2"
shift 2
echo "Watching directory $dirn for changes of file $filen . Watching directory insteaf of file to overcome behaviour of many text editors that replace file - Thanks to Giles and see his answer https://superuser.com/a/181543/81861 for more details. In case of matching even I execute:" $@
#inotifywait -m -e close_write,moved_to,create "$dirn" |
#inotifywait -m -e close_write "$dirn" |
#while read -r directory events filename; do
inotifywait -m -e close_write --format '%f' "$dirn" |
while read -r filename; do
echo "[inotifywait] "$directory $events $filename
if [ "$filename" = "$filen" ]; then
echo "[executing] " "$@"
"$@"
else
echo "[not executing] Due to mismatch in filename. Expected \"$filen\", but received event about \"$filename\""
fi
done
echo loop reading from inotifywait finished with status $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment