watchcat: refactor function names

Signed-off-by: Nicholas Smith <nicholas@nbembedded.com>
This commit is contained in:
Nicholas Smith
2020-12-13 16:01:17 +10:00
committed by Nicholas Smith
parent dc33ae8027
commit ddd2729313
3 changed files with 116 additions and 96 deletions
+38 -27
View File
@@ -10,56 +10,67 @@ reboot_now() {
[ "$1" -ge 1 ] && {
sleep "$1"
echo 1 > /proc/sys/kernel/sysrq
echo b > /proc/sysrq-trigger # Will immediately reboot the system without syncing or unmounting your disks.
echo 1 >/proc/sys/kernel/sysrq
echo b >/proc/sysrq-trigger # Will immediately reboot the system without syncing or unmounting your disks.
}
}
watchcat_always() {
local period="$1"; local forcedelay="$2"
watchcat_periodic() {
local period="$1"
local forcedelay="$2"
sleep "$period" && reboot_now "$forcedelay"
}
watchcat_ping() {
local period="$1"; local forcedelay="$2"; local pinghosts="$3"; local pingperiod="$4"; local nopingtime="$5"
local period="$1"
local forcedelay="$2"
local pinghosts="$3"
local pingperiod="$4"
local nopingtime="$5"
local time_now="$(cat /proc/uptime)";time_now="${time_now%%.*}"
local time_now="$(cat /proc/uptime)"
time_now="${time_now%%.*}"
[ "$time_now" -lt "$nopingtime" ] && sleep "$((nopingtime-time_now))"
[ "$time_now" -lt "$nopingtime" ] && sleep "$((nopingtime - time_now))"
time_now="$(cat /proc/uptime)";time_now="${time_now%%.*}"
time_now="$(cat /proc/uptime)"
time_now="${time_now%%.*}"
local time_lastcheck="$time_now"
local time_lastcheck_withinternet="$time_now"
while true
do
while true; do
# account for the time ping took to return. With a ping time of 5s, ping might take more than that, so it is important to avoid even more delay.
time_now="$(cat /proc/uptime)"; time_now="${time_now%%.*}"
local time_diff="$((time_now-time_lastcheck))"
time_now="$(cat /proc/uptime)"
time_now="${time_now%%.*}"
local time_diff="$((time_now - time_lastcheck))"
[ "$time_diff" -lt "$pingperiod" ] && sleep "$((pingperiod-time_diff))"
[ "$time_diff" -lt "$pingperiod" ] && sleep "$((pingperiod - time_diff))"
time_now="$(cat /proc/uptime)";time_now="${time_now%%.*}"
time_now="$(cat /proc/uptime)"
time_now="${time_now%%.*}"
time_lastcheck="$time_now"
for host in $pinghosts
do
if ping -c 1 "$host" &> /dev/null
then
for host in $pinghosts; do
if ping -c 1 "$host" &>/dev/null; then
time_lastcheck_withinternet="$time_now"
else
logger -p daemon.info -t "watchcat[$$]" "no internet connectivity for $((time_now-time_lastcheck_withinternet)). Reseting when reaching $period"
else
logger -p daemon.info -t "watchcat[$$]" "no internet connectivity for $((time_now - time_lastcheck_withinternet)). Reseting when reaching $period"
fi
done
[ "$((time_now-time_lastcheck_withinternet))" -ge "$period" ] && reboot_now "$forcedelay"
[ "$((time_now - time_lastcheck_withinternet))" -ge "$period" ] && reboot_now "$forcedelay"
done
}
if [ "$1" = "always" ]
then
watchcat_always "$2" "$3"
else
case "$mode" in
periodic_reboot)
watchcat_periodic "$2" "$3"
;;
ping_reboot)
watchcat_ping "$2" "$3" "$4" "$5" "$6"
fi
;;
*)
echo "Error: invalid mode selected: $mode"
;;
esac