Stage 1: Activating watchdog hardware in pi

Open the following file:

sudo nano /boot/config.txt

and at the end of this file add following lines:

# Enabling watchdog.
dtparam=watchdog=on

Reboot your raspberry pi. After reboot list devices with the name prefixed by watchdog, to do so run the following command:

ls -al /dev/watchdog*

It will display output like this:

crw------- 1 root root  10, 130 May 19 07:09 /dev/watchdog
crw------- 1 root root 253,   0 May 19 07:09 /dev/watchdog0

There are two devices that you can see above.

If you use one of the watchdog's, trying to use the other one gives you a permission error. Bottom line, there was and is only one hardware watchdog provided.

Stage 2: Installing watchdog

To install watchdog run this command on pi's terminal

sudo apt-get install watchdog

Once finished this installation will add the necessary systemd files, to verify them run this command.

ls -l /lib/systemd/system/

and you will get the output something like below on your terminal.

watchdog.service
wd_keepalive.service

The configuration file created for watchdog is this, we will not edit this for now as it looks perfect for now.

ls -al /etc/default/watchdog
# Start watchdog at boot time? 0 or 1
run_watchdog=1
# Start wd_keepalive after stopping watchdog? 0 or 1
run_wd_keepalive=1
# Load module before starting watchdog
watchdog_module="none"
# Specify additional watchdog options here (see manpage).

Stage 3: Configuring watchdog to respond to events

To add your configuration edit this file

nano /etc/watchdog.conf

Uncomment the following lines or add them in the bottom of the files if you don't find these lines in the above file.

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

There is one more line that I added at the end of the above file is this.

watchdog-timeout=15

The reason behind it is that it was throwing me below error when I run status check on watchdog :

cannot set timeout 60 (errno = 22 = 'Invalid argument')

Stage 4: Starting/Monitoring watchdog service:

To start the service:

sudo systemctl start watchdog

To check watchdog status:

sudo systemctl status watchdog

if you get cannot set timeout error please see the fix at the end of the above section.

To stop the service use this command:

sudo systemctl stop watchdog

Stage 5: Add watchdog on boot.

Edit the following file

/lib/systemd/system/watchdog.service

and add the following lines under the Install section.

[Install]
WantedBy=multi-user.target

save it and run this command:

sudo systemctl enable watchdog