Bash - Watch Directory and run Command on Changes

#!/bin/bash #./watch.sh $PATH $COMMAND DIR=$1 COMMAND=$2 SLEEP=2 # seconds daemon() { echo "Daemon Started" echo "Watching $DIR" echo "Will Run: $COMMAND" echo chsum1=`find $DIR -type f -exec md5 {} \;` # initial value while [[ true ]] do chsum2=`find $DIR -type f -exec md5 {} \;` if [[ $chsum1 != $chsum2 ]] ; then chsum1=$chsum2 echo -e "-> Files Changed" eval $COMMAND fi sleep $SLEEP done } daemon : ' ./watch.sh './' 'echo -e "\tRun Your Command Here"' Daemon Started Watching ./ Will Run: echo -e "\tRun Your Command Here" -> Files Changed Run Your Command Here -> Files Changed Run Your Command Here -> Files Changed Run Your Command Here -> Files Changed Run Your Command Here CTRL + C to exit '
./watch.sh './' 'echo -e "\tRun Your Command Here"'

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.