X-Git-Url: https://git.friedersdorff.com/?a=blobdiff_plain;f=states%2Futils%2Ffiles%2Fonchanges.bash;fp=states%2Futils%2Ffiles%2Fonchanges.bash;h=d7c6bb93c9a986abcd7d06582a9ed695edea7596;hb=5116eb11c2eb96dafd84fc5663fd6164452e28d3;hp=0000000000000000000000000000000000000000;hpb=3fc1ee210875b42d272698011e3ab276e41146ba;p=max%2Fsaltfiles.git diff --git a/states/utils/files/onchanges.bash b/states/utils/files/onchanges.bash new file mode 100644 index 0000000..d7c6bb9 --- /dev/null +++ b/states/utils/files/onchanges.bash @@ -0,0 +1,51 @@ +#!/bin/bash +# +# Watch current directory (recursively) for file changes, and execute +# a command when a file or directory is created, modified or deleted. +# +# Written by: Senko Rasic +# +# Requires Linux, bash and inotifywait (from inotify-tools package). +# +# To avoid executing the command multiple times when a sequence of +# events happen, the script waits one second after the change - if +# more changes happen, the timeout is extended by a second again. +# +# Installation: +# chmod a+rx onchange.sh +# sudo cp onchange.sh /usr/local/bin +# +# Example use - rsync local changes to the remote server: +# +# onchange.sh rsync -avt . host:/remote/dir +# +# Released to Public Domain. Use it as you like. +# + +EVENTS="CREATE,CLOSE_WRITE,DELETE,MODIFY,MOVED_FROM,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 $@ + eval $@ + done +)