-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.sh
More file actions
executable file
·68 lines (61 loc) · 1.26 KB
/
monitor.sh
File metadata and controls
executable file
·68 lines (61 loc) · 1.26 KB
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
#!/bin/sh
# $1: the file or folder to monitor
# $2: the script to execute when the file/folder changes
#
# If only one argument is received, it will be used as the script to execute and
# the folder in which the script is located will be monitored
get_folder_of() {
file_path=$1
# File path
fn=${fn:-$(readlink -f "$file_path" 2>/dev/null)}
# Dir name
echo ${fn%/*}
}
die() {
echo $@ >&2
exit 1
}
test_only=false
end_of_opts=false
while [ $end_of_opts = false ]; do
case $1 in
-t|--test)
test_only=true
shift
;;
--)
end_of_opts=true
shift
;;
-*)
die 'Unknown option: '$1
;;
*)
end_of_opts=true
;;
esac
done
[ $# -ne 0 ] || die "Not enough args!"
mon_files=
script=
if [ $# -eq 1 ]; then
if [ -d $1 ]; then
mon_files=$1/*
elif [ -f $1 ]; then
mon_files=${1%/*}/*
fi
script=$1
elif [ $# -eq 2 ]; then
if [ -f $1 ]; then
mon_files=$1
elif [ -d $1 ]; then
mon_files=$1/*
fi
script=$2
else
die 'Too many args. Expected max 2.'
fi
[ -n "$mon_files" ] || die 'Error: no files to monitor.'
[ $test_only = false ] || runpy_opts="${runpy_opts:+$runpy_opts }-t"
# Keep monitoring the files until end of world or until a signal is received
echo $mon_files | tr ' ' '\n' | entr -c $(get_folder_of $0)/runpy.sh $runpy_opts $script