From 906688f458b5f6376900d84c417cf3acffda91b4 Mon Sep 17 00:00:00 2001 From: iamromulan <50184035+iamromulan@users.noreply.github.com> Date: Mon, 29 Apr 2024 23:00:27 -0400 Subject: [PATCH] Create ping watchdog --- simpleadmin/console/services/ping_watchdog.sh | 28 +++++++++++++++++++ .../services/systemd/ping_watchdog.service | 14 ++++++++++ 2 files changed, 42 insertions(+) create mode 100644 simpleadmin/console/services/ping_watchdog.sh create mode 100644 simpleadmin/console/services/systemd/ping_watchdog.service diff --git a/simpleadmin/console/services/ping_watchdog.sh b/simpleadmin/console/services/ping_watchdog.sh new file mode 100644 index 0000000..6662402 --- /dev/null +++ b/simpleadmin/console/services/ping_watchdog.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +# Define the hostname or IP address to ping +HOSTNAME="google.com" + +# Number of pings to attempt +PING_COUNT=6 + +# Initialize a counter for successful pings +success_count=0 + +# Attempt to ping the specified number of times +for i in $(seq 1 $PING_COUNT); do + # Ping the hostname with a timeout of 1 second per ping + if ping -c 1 -W 1 $HOSTNAME &> /dev/null; then + ((success_count++)) + else + echo "Ping attempt $i failed." + fi +done + +# Check if all pings failed +if [ $success_count -eq 0 ]; then + echo "All $PING_COUNT ping attempts failed, executing AT command." + /bin/atcmd 'AT+CFUN=1,1' +else + echo "$success_count out of $PING_COUNT ping attempts were successful." +fi diff --git a/simpleadmin/console/services/systemd/ping_watchdog.service b/simpleadmin/console/services/systemd/ping_watchdog.service new file mode 100644 index 0000000..3ebe17b --- /dev/null +++ b/simpleadmin/console/services/systemd/ping_watchdog.service @@ -0,0 +1,14 @@ +[Unit] +Description=Ping Watchdog Service +Wants=network.target +After=network.target + +[Service] +Type=simple +ExecStartPre=/bin/sleep 60 # Sleep for 60 seconds to ensure the network is ready +ExecStart=/usrdata/simpleadmin/console/services/ping_watchdog.sh +Restart=on-failure +RestartSec=30s + +[Install] +WantedBy=multi-user.target