CPE网络中断自动重启

家里CPE老是时不时就没网了,需要重启才好使,每次手动重启有点麻烦,今天写个脚本。

vi /etc/check_network.sh

写入

#!/bin/sh
TARGET="223.5.5.5"
INTERVAL=1
FAIL_COUNT=0
MAX_FAIL=10
LOG_FILE="/opt/network_check.log"

while true; do
    if ping -c 1 $TARGET > /dev/null 2>&1; then
        FAIL_COUNT=0
    else
        FAIL_COUNT=$((FAIL_COUNT + 1))
        echo "$(date): Network is down, fail count: $FAIL_COUNT" >> $LOG_FILE
    fi

    if [ $FAIL_COUNT -ge $MAX_FAIL ]; then
        echo "$(date): $MAX_FAIL consecutive ping failures, rebooting..." >> $LOG_FILE
        /sbin/reboot
        FAIL_COUNT=0
    fi

    sleep $INTERVAL
done

这个脚本是每隔1秒检测一下到223.5.5.5通不通,连续10次不通就重启。启动脚本

vi /etc/init.d/check_network

写入

#!/bin/sh /etc/rc.common
START=99

start() {
    /etc/check_network.sh &
}

自启动

/etc/init.d/check_network enable
/etc/init.d/check_network start

阅读剩余
THE END