avatar

tetsai

原创声明

本文由tetsai原创,转载请注明来源

最近用树梅派挂了点东西,老是容易死机。所以我百度搜了一下发现有看门狗这个东西,但大多的都是老教程,这里给一个一步到位的教程也当是个记录

sudo su modprobe bcm2708_wdog echo "bcm2708_wdog" >> /etc/modules sudo apt-get install watchdog chkconfig

最后是我的配置 /etc/watchdog.conf文件(不要直接**!有几处要改!)

ping = 192.168.1.3 #ping = 172.26.1.255 interface = eth0 #file = /var/log/messages #change = 1407 # Uncomment to enable test. Setting one of these values to '0' disables it. # These values will hopefully never reboot your machine during normal use # (if your machine is really hung, the loadavg will go much higher than 25) max-load-1 = 24 #max-load-5 = 18 #max-load-15 = 12 # Note that this is the number of pages! # To get the real size, check how large the pagesize is on your machine. #min-memory = 1 #allocatable-memory = 1 #repair-binary = /usr/sbin/repair #repair-timeout = 60 #test-binary = #test-timeout = 60 # The retry-timeout and repair limit are used to handle errors in a more robust # manner. Errors must persist for longer than retry-timeout to action a repair # or reboot, and if repair-maximum attempts are made without the test passing a # reboot is initiated anyway. retry-timeout = 60 repair-maximum = 1 watchdog-device = /dev/watchdog watchdog-timeout = 10 # Defaults compiled into the binary #temperature-sensor = #max-temperature = 90 # Defaults compiled into the binary #admin = root #interval = 1 #logtick = 1 #log-dir = /var/log/watchdog # This greatly decreases the chance that watchdog won't be scheduled before # your machine is really loaded realtime = yes priority = 1 # Check if rsyslogd is still running by enabling the following line #pidfile = /var/run/rsyslogd.pid 

其中

ping = 192.168.1.3 interface = eth0

相当于网络检测,意思就是说如果eth0的192.168.1.3这个ip一旦ping不通,则触发自动重启,可以说是软看门狗了


max-load-1 = 24

max-load-1是最大进程数,不能超过24个进程(感觉没什么用啊,我用fork炸弹炸了一下,也没触发它,懂的人可以说一下为什么)


retry-timeout = 60 repair-maximum = 1

不知道是什么东西,从英语上来看似乎是重试次数,我把它改成10,树梅派开都开不了机(还没开机就重启),改成60后,发现它可以识别fork炸弹了,炸了一下60秒后就自动重启了。


watchdog-device = /dev/watchdog watchdog-timeout = 10

这两个必须写明白,watchdog-device默认是/dev/watchdog就好

watchdog-timeout必须是10~15的值,坑人的是它默认值是60,必须指定!太大无法启动看门狗(似乎是硬件限制)

相关文章:https://www.raspberrypi.org/forums/viewtopic.php?t=244843


剩下的就没有什么要注意的了,启动它即可

sudo chkconfig watchdog on sudo /etc/init.d/watchdog start

最后是检查看门狗是不是起来了

[email protected]:/home/pi# systemctl status watchdog ● watchdog.service - watchdog daemon Loaded: loaded (/lib/systemd/system/watchdog.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2020-05-15 19:31:58 CST; 5min ago Process: 3229 ExecStartPre=/bin/sh -c [ -z "${watchdog_module}" ] || [ "${watchdog_module}" = "none" ] || /sbin/modp Process: 3230 ExecStart=/bin/sh -c [ $run_watchdog != 1 ] || exec /usr/sbin/watchdog $watchdog_options (code=exited, Main PID: 3232 (watchdog) Tasks: 1 (limit: 2200) Memory: 760.0K CGroup: /system.slice/watchdog.service └─3232 /usr/sbin/watchdog

看起来是OK的,注意如果报错,什么watchdog error: cannot set timeout 60 (errno = 22 = 'Invalid argument'),注意你的watchdog-timeout设置了没,必须小于等于15才可以。

让树梅派死机

总不能等它死机了看看正常不正常吧?那得等到什么时候!

这里分享两个死机代码

sudo su echo c > /proc/sysrq-trigger

会向linux内核发送一个**,这个**会直接让整个系统陷入**状态(彻底**,基本上就是人死了,瞳孔反射都没了那种)

这种会触发看门狗的watchdog-timeout(也就是说只需要等10秒就会自动重启)

还有一个叫fork炸弹,会无限创建进程,把系统挤死,但此时系统死的不彻底,不会触发watchdog-timeout,但是会触发retry-timeout(也就是说,你要等60秒才能看见它自动重启)

:() { :|:& };:

发表评论

电子邮件地址不会被公开。 必填项已用*标注