]> git.donarmstrong.com Git - bin.git/blob - run_when_changed
support no inotifywait
[bin.git] / run_when_changed
1 #!/bin/sh
2
3 WATCH_FULL_FILE="$1"
4 shift 1;
5
6 if ! [ -e "$WATCH_FULL_FILE" ]; then
7     echo "No such file or directory $WATCH_FULL_FILE";
8 fi;
9
10 WATCH_DIR=`dirname "$WATCH_FULL_FILE"`
11 WATCH_FILE=`basename "$WATCH_FULL_FILE"`
12
13 if which inotifywait > /dev/null 2>&1; then
14     inotifywait -m -e close_write,moved_to,create $WATCH_DIR |
15         while read -r directory events filename; do
16             if [ "x$filename" = "x$WATCH_FILE" ]; then
17                 ( exec "$@" );
18             fi;
19         done;
20 else
21     CURR_TIME=$(stat -c '%Y' "$WATCH_FULL_FILE");
22     while sleep 1; do
23         NEW_TIME=$(stat -c '%Y' "$WATCH_FULL_FILE");
24         if [ "x$NEW_TIME" != "x$CURR_TIME" ]; then
25             CURR_TIME=$NEW_TIME
26             ( exec "$@" );
27         fi;
28     done;
29 fi;