#!/sbin/openrc-run description="Dead‑man switch: ping a host, restart a service if ping fails" # The services you want to restart SERVICES="wg-quick.wg0" PING_HOST="10.100.0.2" FAIL_COUNT_LIMIT=5 PING_TIMEOUT=1 PING_COUNT=1 restart_service() { for i in ${SERVICES} do einfo "Restarting $i" /etc/init.d/$i restart done } depend() { need net } start() { ebegin "Starting tunnel-watchdog daemon" while : ; do # Perform a quick ping. -q quiet, -c N packets, -W T timeout if ! ping -q -c ${PING_COUNT} -W ${PING_TIMEOUT} ${PING_HOST} >/dev/null 2>&1; then fail_count=$((fail_count + 1)) echo "Ping to ${PING_HOST} failed (attempt ${fail_count})" else fail_count=0 fi # If we hit the threshold, restart if [ "${fail_count}" -ge "${FAIL_COUNT_LIMIT}" ]; then echo "Consecutive failures reached ${FAIL_COUNT_LIMIT}: restarting ${SERVICES}" restart_service fail_count=0 fi # Wait a bit before the next check sleep 5 done & PID=$! eend 0 } stop() { ebegin "Stopping ping‑restart daemon" if [ -n "${PID}" ] && kill -0 "${PID}" 2>/dev/null; then kill "${PID}" wait "${PID}" fi eend 0 }