For one of my applications, I needed a watchdog, so I turned to my own post (this one) that I did a number of years ago.
Fortunately, things have progressed since then, and a lot of information has been uncovered or excavated by many contributors. Instead of asking the question in the title if the methods I used was right, we can now be more specific, which is why I adjusted the title of the post somewhat.
Because this post is still getting quite a few hits, I decided to redo this part completely. I hesitated earlier, because most if not all of the subsequent inputs to this post will be made irrelevant. I did not want to impede on the work others did to help with this subject, but the whole description was becoming extremely confusing, even to me

So, here is a quick and concise summary of the various ways to use the watchdog functionality.
After all the trouble some of us went through to master the watchdog, it basically distilled down to three different methods.
These three methods cannot be combined because the /dev/watchdog device is claimed by either of the methods.
The watchdog device is already activated at boot time for all three methods.
I tried Method 1 and Method 2, which are RPi specific, on an RPi Model B3+ running Stretch, and on the RPi Model 4 running Buster. Both methods work fine on either RPi.
Method 1
The easy shell method is as follows:
With a little script, you can add protection for kernel and user-space program hang-ups.
You start that process by sending a period "." to /dev/watchdog. This will kick-off what I would call a keep-alive session. You, or your program now needs to continue to send a "." to the /dev/watchdog within a 15 second period. If you don't, the RPi will reboot automatically. You can send the character "V" to the device to cancel this process.
You can use the following command to test this out - watch out however, the RPi will reboot in 15 seconds if this is all you do! :
Code: Select all
sudo sh -c "echo '.' >> /dev/watchdog"
Creating and activating the following little script (from user sparky777), will protect the RPi for kernel hang-ups.
Code: Select all
#!/bin/bash
echo " Starting user level protection"
while :
do
sudo sh -c "echo '.' >> /dev/watchdog"
sleep 14
done
I took the easy way and installed it with cron. Just add
and reboot to install.@reboot /home/pi/name-of-program
When this script runs, there is now protection for kernel related issues. This can be tested with the so called fork bomb.
Make sure the script runs.
Simply type the following sequence at a prompt and then hit return to launch the fork-bomb.
Code: Select all
: ( ){ : | : & }; :
Method 2
The second method with the same functionality can be obtained by using systemd.
To let systemd use the watchdog, and to turn it on, you need to edit the systemd configuration file.
Code: Select all
sudo nano /etc/systemd/system.conf
to:#RuntimeWatchdogSec=
Fifteen seconds is the maximum the BCM hardware allows.RuntimeWatchdogSec=10s
I also suggest you activate the shutdown period protection by removing the '#' in front of the next line.
ShutdownWatchdogSec=10min
After a reboot, this will activate and reserve the watchdog device for systemd use. You can check the activation with :
Code: Select all
dmesg | grep watchdog
The kernel will now update the hardware watchdog automatically every 10/2 seconds. If there is no kernel activity for 10 seconds, the RPi reboots.[ 0.784298] bcm2835-wdt 3f100000.watchdog: Broadcom BCM2835 watchdog timer
[ 1.696537] systemd[1]: Hardware watchdog 'Broadcom BCM2835 Watchdog timer', version 0
[ 1.696628] systemd[1]: Set hardware watchdog to 10s.
This means that there is a default protection for kernel related issues. This can be tested with the so called fork bomb, see above.
If you want the user-space application protection capability, you have to use the systemd API within your program to do that. This is covered in a later post.
Method 3
The third method is not RPi specific and uses a rather large and sophisticated daemon package (pretty much legacy now) that allows you to set many different parameters that will be able to reboot the RPi. After installation you can use
Code: Select all
man watchdog
The package needs to be installed first.
Code: Select all
sudo apt-get install watchdog
To set some of the parameters the watchdog daemon should watch :
Code: Select all
nano /etc/watchdog.conf
The last line is very important and Rpi specific. If this command is not added, you get a bit of a cryptic error (run sudo systemctl status watchdog.service) :# This is an optional test by pinging my router
ping=192.168.1.1
max-load-1 = 24
min-memory = 1
watchdog-device = /dev/watchdog
watchdog-timeout = 15
This is caused by the default wdt counters used in other Linux systems, because the RPi wdt counter on the SOC only handles a maximum of 15 seconds. Unfortunately, this is a bug that the Foundation missed and should have been programmed into the kernel, or added by default in the watchdog.conf file.cannot set timeout 60 (errno = 22 = 'Invalid argument')
In a follow-up post I will show how to add extra support for your own (Python) application by using the systemd API and framework.
Enjoy!