-
Notifications
You must be signed in to change notification settings - Fork 9
/
batch.sh
65 lines (53 loc) · 1.74 KB
/
batch.sh
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
#!/bin/bash
# ---------------------------------------------------
# A bash script to watch multiple URLs at once, based
# on a simple JSON config file.
# Can also be used in a CRON job.
#
# Prerequisites:
# --------------
# - jq and bc must be installed
# - Debian / Ubuntu: apt install jq bc
# - Fedora / RHEL: dnf install jq bc
# - watcher.py must be executable
# - chmod +x watcher.py
# - batch.sh must be executable
# - chmod +x batch.sh
#
# Example Usage:
# --------------
# ./batch.sh example/many.json --adapter stdout
# ---------------------------------------------------
jobs="$(cat $1 | jq -c '.[]')"
total=$(jq length $1)
cur=1
# https://unix.stackexchange.com/a/9789
while IFS= read -r job; do
url=$(echo $job | jq -r '.url')
tolerance=$(echo $job | jq -r '.tolerance')
xpath=$(echo $job | jq -r '.xpath')
ignore=$(echo $job | jq -r '.ignore' | tr \; " ")
if [ "$url" == "null" ]; then
echo "Error: URL parameter missing for job $cur of $total"
exit 1
fi
if [ "$tolerance" == "null" ]; then
echo "Error: Tolerance parameter missing for job $cur of $total"
exit 1
fi
if [ "$xpath" == "null" ]; then
echo "Error: XPATH missing for job $cur of $total (just set it to '/' to watch whole document)"
exit 1
fi
if [ "$ignore" == "null" ]; then
echo "Error: XPATH ignore list missing for job $cur of $total (separate XPATH expressions by a space)"
exit 1
fi
echo "[$cur/$total] Watching $url."
start=`date +%s.%N`
./watcher.py -u "$url" -t "$tolerance" -x "$xpath" -i $ignore ${@:2}
end=`date +%s.%N`
echo "[$cur/$total] Took $(echo $end-$start | bc) seconds."
echo ""
cur=$((cur+1))
done < <(printf '%s\n' "$jobs")