aboutsummaryrefslogtreecommitdiff
path: root/inotifier
blob: c4d7098a7b0e2268cecc6edfc103b62faef54f66 (plain)
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
#!/usr/bin/env bash
#
# Run commands when a folder content changes.
#
# Alternatives:
#
# * https://github.com/quitesimpleorg/adhocify
# * https://tracker.debian.org/pkg/inotify-hookable
# * https://metacpan.org/dist/App-Inotify-Hookable

# Parameters
BASENAME="`basename $0`"
WATCHED="$1"
COMMAND="$2"

# Listened inotify events
#
# See inotifywait(0) for the full list and description of all supported events.
#
# By default, exclude "access", "close", "close_write", "close_nowrite", "open"
# from the list of events
INOTIFY_EVENTS="-e modify -e attrib -e moved_to -e moved_from -e move -e move_self -e create -e delete -e delete_self -e unmount"

# Check
if [ -z "$COMMAND" ]; then
  echo "usage: $BASENAME <folder> <command> [args]"
  exit 1
elif [ ! -e "$WATCHED" ]; then
  echo "error: file or folder not found: $WATCHED"
  exit 1
fi

# Shift
shift 2

# Normalize folder name
if [ -d "$WATCHED" ]; then
  WATCHED="`cd $WATCHED &> /dev/null && pwd`"
fi

# UX
echo "Watching $WATCHED to exec \"$COMMAND $*\" upon changes..."

# Dispatch
# Excluding any .git folder from being watched
while inotifywait $INOTIFY_EVENTS --exclude '.*.git.*' -r $WATCHED; do
  $COMMAND $*
done