-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch-run
executable file
·56 lines (48 loc) · 1.19 KB
/
watch-run
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
#!/bin/bash
set -u
usage(){
cat <<EOF
$(basename $0) /path/to/file-to-be-watched [--notify] command [...args]
EOF
}
### Set initial time of file
file=${1:-}
[[ -n $file ]] || { echo "Filename to be watched must be supplied"; usage; exit 2; }
shift
notification_enabled=
if [[ "${1:-}" = "--notify" ]]; then
notification_enabled=true
shift
fi
first_notification=true
notify(){
local pfx=
[[ "$first_notification" = true ]] || pfx="re"
notify-send "debug-ls" "$(basename $file) ${pfx}started."
first_notification=false
}
cmd=${@:-}
[[ -n $cmd ]] || { echo "Command must be supplied"; exit 2; }
LTIME=
CMD_PID=
cleanup(){
[[ -n $CMD_PID ]] && kill $CMD_PID
}
trap cleanup EXIT
while :; do
ATIME=`stat -c %Z "$file"`
if [[ "$ATIME" != "$LTIME" ]]; then
#echo "RUN COMMAND, $ATIME"
if [[ -n $CMD_PID ]]; then
echo "---------------------------------------"
echo "| Restarting... |"
echo "---------------------------------------"
kill $CMD_PID
fi
[[ "$notification_enabled" = "true" ]] && notify
$cmd &
CMD_PID=$!
LTIME=$ATIME
fi
sleep 2
done