Tuesday, October 6, 2015

Watchdog for Raspberry Pi

Maybe not all of you know that Raspberry Pi has an integrated watchdog timer. This can be really useful, especially when you need to leave your RPi unattended for some time.

For those who doesn't know what is a watchdog, let's see just a small explanation...

Immagine that you made an incredibily wonderful home surveillance system and you are going to use it to check your house during an long trip abroad. After a bunch of days, something goes wrong and your system hangs, so it become unuseful. The only way to solve this problem would be to reboot the system, but you are away from it...

Watchdogs do this reboot for you.

From wikipedia: "A watchdog timer is an electronic timer that is used to detect and recover from computer malfunctions. During normal operation, the computer regularly restarts the watchdog timer to prevent it from elapsing, or 'timing out'. If, due to a hardware fault or program error, the computer fails to restart the watchdog, the timer will elapse and generate a timeout signal. The timeout signal is used to initiate corrective action or actions. The corrective actions typically include placing the computer system in a safe state and restoring normal system operation."

The RPi cpu has several programmable timers and one of this can be used as a watchdog timer. Configuring the watchdog it's quite simple, but there are few things to keep in mind...

This watchdog cannot restore a system in every situation. If your system stops responding because of a loss of power (so it has been actually shut down), the timer also stops and it cannot do its job.

There could be also a problem when your software hangs or stops working because of an error, but the RPi is still working well. In this case the signal is still sent to the timer, even if you cannot access your program anymore.

Now we can enable the watchdog by adding a line to the file /etc/modules:

sudo nano /etc/modules

then add the following line, save and exit:


At the next reboot you will have the watchdog device enabled, but still not working.

Proceed by installing the management software:

sudo apt-get install watchdog
sudo update-rc.d watchdog defaults

and edit the file ​/etc/watchdog.conf removing the character # from two lines:

#max-load-1 = 24
#watchdog-device = /dev/watchdog

should become

max-load-1 = 24
watchdog-device = /dev/watchdog

After saving you can reboot and the watchdog will work.

Instead of using the max-load parameter, you could specify the timeout of the timer in seconds inserting in the file /etc/watchdog.conf the following lines:

watchdog-timeout = 15
interval = 5

and adding the # in front of the max-load line, if you removed it. These lines set the timeout to 15 seconds and the reset of the timer every 5 seconds. You can of course change these values to adapt to your needs.

As a footnote, it could be useful to also enable ssh connection to your RPi from internet (change the default password before doing this!!). This way you can also try to connect when you are away from the Raspberry and sometimes solve the problem if the watchdog cannot do it for you.

1 comment:

  1. Interesting idea as I do have this issue (home cams). I have tried a cronjob rather than a watchdog for the reboot - again, the cronjob could seize up with a serious crash.