What's new

Tutorial Automatically Reboot Asus Router on Network Failures

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

bg604

Occasional Visitor
I wrote a script that checks the connectivity of several IP addresses (Google DNS, Cloudflare, and Quad9) by pinging them up to three times. If any ping is successful, the script exits.

If all attempts fail, it triggers a reboot, which can resolve many common network issues like temporary ISP outages and helps ensure your router stays connected to the internet without requiring manual intervention.

Please let me know what you think, first time doing something like this.

Step 1: Enable Persistent JFFS2 partition and SSH
  1. Log into Asus router – 192.168.xxx.xxx and log in using your credentials.
  2. Navigate to Administration -> System tab.
  3. In Persistent JFFS2 partition section:
    • Enable JFFS custom scripts and configs – Yes.
  4. In Service section:
    • Enable SSH - LAN only.
    • Allow SSH Port Forwarding - No.
    • Set SSH Port - 22.
    • Enable Allow Password Login - Yes.
    • Leave Authorized Keys blank.
    • Set Idle Timeout - 20.
Step 2: Install an SSH client on Windows
  1. Download and install PuTTY (https://www.putty.org/)
Step 3: Connect to your router via SSH using PuTTY
  1. Open PuTTY.
  2. Enter Host Name: 192.168.xxx.xxx.
  3. Set Port: 22
  4. Select Connection: SSH
  5. Click Open and confirm the security alert.
  6. Log in using your Asus router credentials.
Step 4: Create the script file
  1. Open PuTTY and run the following command:
Bash:
vi /jffs/scripts/ping_reboot.sh

Step 5: Enter Insert Mode
Press i to enter insert mode in the editor

Step 6: Copy and paste the script

Bash:
#!/bin/sh
# Log file location on /jffs (persistent across reboots)
LOG_FILE="/jffs/ping_reboot.log"

# Maximum log size in bytes (e.g., 512KB = 524288 bytes)
MAX_LOG_SIZE=524288

# Number of ping attempts per target
ATTEMPTS=3

# Ping targets (space-separated instead of an array)
PING_TARGETS="8.8.8.8 1.1.1.1 9.9.9.9"

# Function to rotate the log if it exceeds the max size
rotate_log() {
    if [ -f "$LOG_FILE" ]; then
        LOG_SIZE=$(stat -c %s "$LOG_FILE")  # Get file size
        if [ "$LOG_SIZE" -ge "$MAX_LOG_SIZE" ]; then
            echo "Log file size exceeds limit. Rotating log..." > "$LOG_FILE"
        fi
    fi
}

echo "Starting the ping test at $(date)"
logger "Starting the ping test at $(date)"  # Log to ASUS GUI system log

# Loop through the ping targets
for PING_TARGET in $PING_TARGETS; do
    echo "Pinging target: $PING_TARGET"
    logger "Pinging target: $PING_TARGET"

    COUNTER=0
    while [ "$COUNTER" -lt "$ATTEMPTS" ]; do
        echo "Attempt $((COUNTER + 1)) to ping $PING_TARGET..."
        logger "Attempt $((COUNTER + 1)) to ping $PING_TARGET..."

        # Try to ping the target
        ping -c 4 "$PING_TARGET" > /dev/null 2>&1
        # Check if the ping was successful
        if [ "$?" -eq 0 ]; then
            echo "Ping to $PING_TARGET succeeded."
            logger "Ping to $PING_TARGET succeeded."
            exit 0
        fi

        # Increment the counter if ping failed
        COUNTER=$((COUNTER + 1))
        rotate_log  # Check if log file size needs rotation
        echo "Ping to $PING_TARGET failed (attempt $COUNTER) at $(date)" >> "$LOG_FILE"
        logger "Ping to $PING_TARGET failed (attempt $COUNTER) at $(date)"  # Log failure to ASUS GUI system log
        # Wait for 5 seconds before the next attempt
        echo "Waiting 5 seconds before the next attempt..."
        sleep 5
    done
done

# If all pings fail, log the failure and reboot
rotate_log  # Check if log file size needs rotation
echo "All pings failed. Rebooting router at $(date)" >> "$LOG_FILE"
logger "All pings failed. Rebooting router at $(date)"  # Log reboot to ASUS GUI system log
echo "Rebooting router due to failed ping attempts..." >> "$LOG_FILE"
logger "Rebooting router due to failed ping attempts..."  # Log reboot message
sleep 10 # Allow time for the log to be written
service reboot

Step 7: Save and exit
  1. After pasting the script, press Esc
  2. Type :wq and hit Enter to save and exit.
Step 8: Permit the script file to be executable
  1. Run the following command:
Bash:
chmod +x /jffs/scripts/ping_reboot.sh

Step 9: Add the Persistent Cron Job​

  1. Open the services-start file for editing:
    Bash:
    vi /jffs/scripts/services-start
  2. Enter insert mode by pressing i and add the following lines:
    Bash:
    #!/bin/sh
    cru a PingReboot "*/5 * * * * /jffs/scripts/ping_reboot.sh"
  3. Save and exit the file by pressing Esc, typing :wq, and pressing Enter.
  4. Make the services-start script executable:
    Bash:
    chmod +x /jffs/scripts/services-start

Step 10: Test the script as-is (run manually)

Bash:
cd /jffs/scripts

Bash:
./ping_reboot.sh

Output should be:
Starting the ping test at DATE
Pinging target: 8.8.8.8
Attempt 1 to ping 8.8.8.8...
Ping to 8.8.8.8 succeeded.

Step 11: Reboot router and verify the Cron Job
1. Reboot router
2. After rebooting the router, check if the cron job is active by running:
Bash:
cru l
You should see:
PingReboot = */5 * * * * /jffs/scripts/ping_reboot.sh #PingReboot#

Step 12: View the log

1. Navigate to the /jffs directory:
Bash:
cd /jffs

2. View the entire log file:
Bash:
cat ping_reboot.log

3. Clear the log (Optional)
Bash:
> ping_reboot.log
 
Last edited:
If all attempts fail, it triggers a reboot, which can resolve many common network issues like temporary ISP outages and helps ensure your router stays connected to the internet without requiring manual intervention.

The common advice for ISP outages is to reboot the modem, not the router.
 
Please let me know what you think, first time doing something like this.
Well done on your script.

I think you forgot to include the last line where it presumably executes a service reboot command.

May I also suggest that you post your script inside a CODE block instead of QUOTE block as it makes it more readable and doesn't mess up the formatting so much.
 
Well done on your script.

I think you forgot to include the last line where it presumably executes a service reboot command.

May I also suggest that you post your script inside a CODE block instead of QUOTE block as it makes it more readable and doesn't mess up the formatting so much.

Thanks - I updated to a CODE block and added reboot
 
and it works! once the router rebooted it reconnected to my ISP modem and WiFi/Internet was up again

here is the log details

Bash:
bg604@GT-AX11000:/tmp/home/root# cat /jffs/ping_reboot.log

Ping to 8.8.8.8 failed (attempt 1) at Tue Jan  7 18:50:13 PST 2025
Ping to 8.8.8.8 failed (attempt 2) at Tue Jan  7 18:50:46 PST 2025
Ping to 8.8.8.8 failed (attempt 3) at Tue Jan  7 18:51:19 PST 2025
Ping to 1.1.1.1 failed (attempt 1) at Tue Jan  7 18:51:52 PST 2025
Ping to 1.1.1.1 failed (attempt 2) at Tue Jan  7 18:52:25 PST 2025
Ping to 1.1.1.1 failed (attempt 3) at Tue Jan  7 18:52:58 PST 2025
Ping to 9.9.9.9 failed (attempt 1) at Tue Jan  7 18:53:31 PST 2025
Ping to 9.9.9.9 failed (attempt 2) at Tue Jan  7 18:54:04 PST 2025
Ping to 9.9.9.9 failed (attempt 3) at Tue Jan  7 18:54:37 PST 2025
All pings failed. Rebooting router at Tue Jan  7 18:54:57 PST 2025
Rebooting router due to failed ping attempts...
 
Last edited:
@bg604,

You dear sir, are a gentleperson and a scholar. Thank you so much for this script, it's going to help me create a few cron jobs which I've been wanting to create for a while.

Thanks again :)

Noob
 
Actually,

Is there a way to post the echos to the main ASUS GUI system log as well? Or would that happen anyway?

Noob
 
Step 6: Copy and paste the script
The first line of your script should be:
Code:
#!/bin/sh

A more appropriate command to reboot in Asuswrt would be:
Code:
service reboot

Step 9: Create the Cron Job to Run the Script Automatically

1. Open the cron jobs with:
Bash:
crontab -e
2. Add the following line:
Bash:
*/5 * * * * /jffs/scripts/ping_reboot.sh #PingReboot#
Editing the crontab directly in Asuswrt means that change would be lost after a reboot. To make a persistent crontab entry in Asuswrt-Merlin place a cru command in /jffs/scripts/services-start as follows:
Code:
#!/bin/sh

cru a PingReboot "*/5 * * * * /jffs/scripts/ping_reboot.sh"
 
Last edited:
Okay thanks... I've made those changes. I was wondering why it would disappear in crontab -e after a reboot. Thanks!


The first line of your script should be:
Code:
#!/bin/sh

A more appropriate command to reboot in Asuswrt would be:
Code:
service reboot


Editing the crontab directly in Asuswrt means that change would be lost after a reboot. To make a persistent crontab entry in Asuswrt-Merlin place a cru command in /jffs/scripts/services-start as follows:
Code:
#!/bin/sh

cru a PingReboot "*/5 * * * * /jffs/scripts/ping_reboot.sh"
 
Actually,

Is there a way to post the echos to the main ASUS GUI system log as well? Or would that happen anyway?

Noob

I've updated the script write to the system log in the Asus GUI using logger
Jan 8 09:55:00 bg604: Starting the ping test at Wed Jan 8 09:55:00 PST 2025
Jan 8 09:55:00 bg604: Pinging target: 8.8.8.8
Jan 8 09:55:00 bg604: Attempt 1 to ping 8.8.8.8...
Jan 8 09:55:03 bg604: Ping to 8.8.8.8 succeeded.
 

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!

Staff online

Top