-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcron.sh
executable file
·58 lines (49 loc) · 973 Bytes
/
cron.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
#!/usr/bin/env ash
# shellcheck shell=dash
DATA_PATH="${DATA_PATH:-/data/speedtest.json}"
INTERVAL="${INTERVAL:-600}"
HEALTHCHECK_TOLERANCE="${HEALTHCHECK_TOLERANCE:-3}"
usage() {
echo "$(basename "$0") run|check"
}
run() {
while true
do
if speedtest --accept-license --accept-gdpr -f json | tee /tmp/speedtest.json
then
# "Publish" result
mv /tmp/speedtest.json "$DATA_PATH"
fi
sleep "$INTERVAL"
done
}
healthcheck() {
local last_speedtest
local now
last_speedtest="$(jq '.timestamp | fromdate' "$DATA_PATH")"
now="$(date '+%s')"
tdiff=$(( now - last_speedtest ))
tolerance=$(( HEALTHCHECK_TOLERANCE * INTERVAL ))
# shellcheck disable=2169
if [[ "$tdiff" -gt "$tolerance" ]]
then
return 1
fi
}
set -e
case "$1" in
run)
run
;;
check|c|healthcheck|--healthcheck|--check|-H)
healthcheck || exit 1
;;
help|h|--help|-h)
usage
exit 0
;;
*)
usage
exit 2
;;
esac