如果一段时间内没有wifi连接,自动重启

如果一段时间内没有wifi连接,自动重启

我的 Raspberry Pi 服务器似乎在随机时间后失去了 wifi 连接,并且不知何故无法自动恢复。

通常,手动重新启动即可解决问题。

我想让它在大约 30 分钟后没有 wifi 时自动重新启动。我怎样才能做到这一点?

答案1

这本质上是沃里克的答案,只是附有分步说明。


  1. 在您的主文件夹中创建以下 shell 脚本:

    check_inet.sh

    #!/bin/bash
    
    TMP_FILE=/tmp/inet_up
    
    # Edit this function if you want to do something besides reboot
    no_inet_action() {
        shutdown -r +1 'No internet.'
    }
    
    if ping -c5 google.com; then
        echo 1 > $TMP_FILE
    else
        [[ `cat $TMP_FILE` == 0 ]] && no_inet_action || echo 0 > $TMP_FILE
    fi
    
  2. 更改权限使其可执行

    $ chmod +x check_inet.sh
    
  3. 编辑/etc/crontabsudo添加以下行(替换yourname为您的实际用户名):

    */30 * * * * /home/yourname/check_inet.sh
    

答案2

一种方法是在 root 的 cron 中添加一个条目,每 30 分钟运行一个脚本。该脚本将测试 WIFI 连接(可能使用 )ping,并将结果写入 /tmp 中的文件 - 1 表示连接存在,0 如果不存在。脚本的后续迭代将检查该文件,如果它为 0,并且 WIFI 连接仍然很差,则运行命令init 6

答案3

我认为 Hololeap 解决方案正在发挥作用。

我的解决方案每 N 分钟检查一次(取决于您配置 crontab 的方式)是否有工作网络连接。如果检查失败,我会跟踪失败情况。当失败计数> 5时,我尝试重新启动wifi(如果wifi重新启动失败,您也可以重新启动Raspberry,请查看评论)。

这是一个始终包含最新版本脚本的 GitHub 存储库: https://github.com/ltpitt/bash-network-repair-automation

根据stackexchange的一般政策(所有答案不应只包含链接),这里还有文件network_check.sh,将其复制并粘贴到您喜欢的任何文件夹中,安装说明位于脚本的注释中。

#!/bin/bash
# Author:
# twitter.com/pitto
#
# HOW TO INSTALL:
#
# 1) Install ifupdown and fping with the following command:
# sudo apt-get install ifupdown fping
#
# 2) Then install this script into a folder and add to your crontab -e this row:
# */5 * * * * /yourhome/yourname/network_check.sh
#
# Note:
# If you want to perform automatic repair fsck at reboot
# remember to uncomment fsck autorepair here: nano /etc/default/rcS

# Let's clear the screen
clear

# Write here the gateway you want to check to declare network working or not
gateway_ip='www.google.com'

# Here we initialize the check counter to zero
network_check_tries=0

# Here we specify the maximum number of failed checks
network_check_threshold=5

# This function will be called when network_check_tries is equal or greather than network_check_threshold
function restart_wlan0 {
    # If network test failed more than $network_check_threshold
    echo "Network was not working for the previous $network_check_tries checks."
    # We restart wlan0
    echo "Restarting wlan0"
    /sbin/ifdown 'wlan0'
    sleep 5
    /sbin/ifup --force 'wlan0'
    sleep 60
    # If network is still down after recovery and you want to force a reboot simply uncomment following 4 rows
    #host_status=$(fping $gateway_ip)
    #if [[ $host_status != *"alive"* ]]; then
    #    reboot
    #fi
}

# This loop will run network_check_tries times and if we have network_check_threshold failures
# we declare network as not working and we restart wlan0
while [ $network_check_tries -lt $network_check_threshold ]; do
    # We check if ping to gateway is working and perform the ok / ko actions
    host_status=$(fping $gateway_ip)
    # Increase network_check_tries by 1 unit
    network_check_tries=$[$network_check_tries+1]
    # If network is working
    if [[ $host_status == *"alive"* ]]; then
        # We print positive feedback and quit
        echo "Network is working correctly" && exit 0
    else
        # If network is down print negative feedback and continue
        echo "Network is down, failed check number $network_check_tries of $network_check_threshold"
    fi
    # If we hit the threshold we restart wlan0
    if [ $network_check_tries -ge $network_check_threshold ]; then
        restart_wlan0
    fi
    # Let's wait a bit between every check
    sleep 5 # Increase this value if you prefer longer time delta between checks
done

编辑 1/26/2018:我删除了临时文件,以便让脚本在内存中运行并避免在 Raspberry 的 SD 卡上写入。

答案4

创建脚本检查连接.sh

#!/bin/bash

ping -c4 www.google.com
let a=$?
if [ "$a" != "0" ]; then
  /sbin/shutdown -r +1 Connection lost, rebooting...
fi

更改权限使其可执行:

chmod +x check_connection.sh

编辑/etc/crontab使用须藤这样它就会每 30 分钟启动之前的脚本:

sudo crontab -e

并添加这一行:

*/30 * * * * /home/yourname/check_connection.sh

相关内容