lesavka/scripts/daemon/lesavka-watchdog.sh

24 lines
549 B
Bash

#!/usr/bin/env bash
set -euo pipefail
HEARTBEAT=/etc/lesavka/watchdog.touch
MAX_AGE_SEC=${LESAVKA_WATCHDOG_MAX_AGE:-840}
if [[ -f /etc/lesavka/no-reboot ]]; then
exit 0
fi
now=$(date +%s)
if [[ ! -f "$HEARTBEAT" ]]; then
logger -t lesavka-watchdog "no heartbeat file; rebooting"
systemctl --no-wall reboot
exit 0
fi
mtime=$(stat -c %Y "$HEARTBEAT" 2>/dev/null || echo 0)
age=$((now - mtime))
if (( age > MAX_AGE_SEC )); then
logger -t lesavka-watchdog "heartbeat stale (${age}s); rebooting"
systemctl --no-wall reboot
fi