-
Notifications
You must be signed in to change notification settings - Fork 1
/
inotify_wrapper
executable file
·70 lines (62 loc) · 1.71 KB
/
inotify_wrapper
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash
set -e -f -u -o pipefail
usage() {
cat <<END_OF_USAGE
Watch the specified files and run the binary when they change.
Usage: $0 file1 [file2 ...] -- binary [args ...]
Files will not be passed to the binary, the caller needs to do that if
required.
END_OF_USAGE
}
main() {
if [[ "$#" -gt 0 ]] && [[ "$1" == "-h" || "$1" == "--help" ]]; then
usage
return 0
fi
local files_to_watch=()
while [[ "$#" -gt 0 && "$1" != "--" ]]; do
files_to_watch+=("$1")
shift
done
## Discard "--"
if [[ "$#" -gt 0 ]]; then
shift
fi
local binary_and_args=("$@")
if [[ "${#files_to_watch[@]}" -eq 0 ]]; then
printf "%s: need at least one file to watch\\n\\n" "$0" >&2
usage >&2
return 1
fi
if [[ "${#binary_and_args[@]}" -eq 0 ]]; then
printf "%s: need a binary (and optionally args) to execute\\n\\n" "$0" >&2
usage >&2
return 1
fi
local use_fswatch=0
if type fswatch >& /dev/null; then
use_fswatch=1
else
if ! type inotifywait >& /dev/null; then
printf "%s: need one of fswatch(1) or inotifywait(1)\\n" "$0" >&2
return 1
fi
fi
"${binary_and_args[@]}" || true
date +"%Y-%m-%d %H:%M:%S"
while true; do
if [[ "${use_fswatch}" -eq 1 ]]; then
fswatch --one-event "${files_to_watch[@]}" > /dev/null
else
# Only trigger on modifications to avoid triggering on programs reading
# the file, like the compiler.
# inotifywait exits unsuccessfully without an error message so we need to
# guard against that with `|| true` :/
inotifywait --quiet --quiet --event modify "${files_to_watch[@]}" || true
fi
printf "\n\n\n\n"
"${binary_and_args[@]}" || true
date +"%Y-%m-%d %H:%M:%S"
done
}
main "$@"