目 录CONTENT

文章目录

【树莓派必备】树莓派看门狗驱动,自动重启防死机

萧瑟
2023-08-22 / 0 评论 / 0 点赞 / 898 阅读 / 2,315 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2023-08-22,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

1、前言

今天在折腾树莓派的时候,不小心把树莓派弄死机了,finalshell也连接不上,重新连接也没有用,只好是断电重启。但如果之后再遇到同样的类型,每次都断电重启,就是电脑也受不了啊。

于是抱着试一试的态度google了一下,发现树莓派是有硬件看门狗的,来保护CPU不至于被烧坏,可以通过安装模块和值守程序来实现看门狗,防止树莓派死机。每次内存或者CPU跑满就会自动重启,虽然不能从根本上杜绝死机,但是能不断电重启对硬件还友好一点。

2、本地环境

服务器:树莓派4B
系统:Debian GNU/Linux 11 aarch64(Py3.7.16)

本文基于宝塔面板讲解,关于宝塔面板更多内容请移步 目前最好用的Linux管理系统 —— 宝塔面板

3、安装看门狗

看门狗硬件已经集成在树莓派中,但是默认没有驱动,需要我们下载对应的驱动才能正常使用。

3.1、查看看门狗硬件版本

网上说的bcm2708_wdog版本在4.3.3内核后就不支持了,现在支持的是bcm2835_wdt,在安装看门狗驱动前查看一下看门狗硬件的版本:

root@pi:~# sudo cat /var/log/kern.log* | grep watchdog

Jul 31 16:17:06 pi kernel: [    1.613346] bcm2835-wdt bcm2835-wdt: Broadcom BCM2835 watchdog timer
grep: (standard input): binary file matches

3.2、安装看门狗驱动

root@pi:~# sudo apt update
[... output ...]
root@pi:~# sudo apt install watchdog
[... output ...]
root@pi:~# sudo systemctl enable watchdog
[... output ...]

3.3、修改配置文件

修改 /boot/config.txt ,在最后边增加一行配置项 dtparam=watchdog=on

修改 /etc/watchdog.conf ,在最后增加以下配置:

max-load-1 = 24
watchdog-device = /dev/watchdog
watchdog-timeout = 15
realtime = yes
priority = 1
temperature-sensor	= /sys/class/thermal/thermal_zone0/temp
max-temperature	= 90

参数含义👇
 
max-load-1 = 24: 代表当系统 1 分钟内的负载高于 24(已经非常非常高了),就重启系统

watchdog-device = /dev/watchdog: 设置看门狗的路径

watchdog-timeout = 15: 15 秒内系统无响应就重启系统,在树莓派 3B 上这个值最高为15。注意不要设置的太小,否则可能造成系统反复重启。

realtime = yes: 看门狗将自己锁定到内存中,因此永远不会被换出

priority = 1: 设置实时模式的计划优先级

temperature-sensor: 获取CPU当前温度,有的设备是temperature-sensor,有的设备是temperature-device,不确定是哪个就在本文件搜索这两个参数,能搜到哪个就用哪个参数

max-temperature = 90:温度超过90度就会引起重启,以保护CPU

4、启动看门狗

root@pi:~# sudo /etc/init.d/watchdog start
Starting watchdog (via systemctl): watchdog.service.

5、设置开机自启动

root@pi:~# sudo systemctl enable watchdog.service
Synchronizing state of watchdog.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable watchdog

6、测试

输入指令:(){ :|: & };: 系统会卡死,不断打印下边内容,过一会儿系统就会重启。

root@pi:~# :(){ :|: & };:
-bash: fork: retry: Resource temporarily unavailable
-bash: fork: retry: Resource temporarily unavailable
-bash: fork: retry: Resource temporarily unavailable
-bash: fork: retry: Resource temporarily unavailable
-bash: fork: retry: Resource temporarily unavailable
-bash: fork: retry: Resource temporarily unavailable
-bash: fork: retry: Resource temporarily unavailable
-bash: fork: retry: Resource temporarily unavailable
-bash: fork: retry: Resource temporarily unavailable
...

7、参考资料

1、https://cloud.tencent.com/developer/article/1813064
2、https://www.jianshu.com/p/811409d71643
3、https://imiku.me/2017/01/19/47.html/

0

评论区