HomeHow ToHow to Setup Watchdog on Raspberry Pi

How to Setup Watchdog on Raspberry Pi

If you are someone who has set always-on Raspberry Pi at a remote location, then you might relate to the problems mentioned below:

  • Raspberry Pi hangs when a power switch happens between the inverter/generator and the main grid.
  • Some scripts consume so much resource that you cannot remotely access the terminal to stop the script or reboot the pi.

This significantly worsens if you have no one at the location to manually power off and power on Raspberry Pi.

In my case, I have set up a Home Assistant Instance in my hometown to make life easier for my grandparents. We have off-grid solar that automatically switches when the grid goes out. This happens more frequently than you might think. My Raspberry Pi setup used to freeze at least once or twice a week, even though we have a UPS inverter.

I was looking for a solution and discovered that the smarty pants at Raspberry Pi Design team already have placed a built-in hardware watchdog just waiting for me to be activated.

Also read: How to Create Torrent Files with Transmission

What is a Watchdog?

As the name suggests, a watchdog is a system hang control scheme. It means that whenever a watchdog does not receive a signal for a specified interval, it will reboot the device, detect the system as frozen, and reboot the system without human intervention.

This service type comes in handy while dealing with 24/7 running machines.

Types of Watchdog

Hardware Watchdog: If the hardware chip is not contacted within the specified interval, it will consider the system stuck or frozen and reboot it. This type of watchdog is more reliable since.

Software Watchdog: As the name suggests, a reliable script is running on a system to check whether the system is working fine or not. However, suppose there is some kind of issue with a malicious or questionable script. In that case, it can cause the software watchdog to fail, and the system might never recover automatically. 

Steps to Enable Hardware Watchdog Timer on Raspberry Pi

Luckily, Raspberry Pi has a built-in hardware watchdog that can be enabled easily with a few steps.

Protect Your Online Privacy With Surfshark
[The VPN that we use here at Tech Arrival]

  1. Type the following command to check whether the watchdog is available on your Raspberry Pi.
ls -al /dev/watchdog*
  1. If you see an output like this, skip to step 5.
Ls -Al /Dev/Watchdog*
  1. Usually, the H/W watchdog is usually enabled by default, but if not, type the following command.
sudo nano /boot/config.txt
Sudo Nano /Boot/Config.txt
  1. Search for “dtparam=watchdog“; if it is not there, add “dtparam=watchdog=on” at the end of the file, save the file, and reboot the system. Repeat the step 1 to check.
  2. Type the following to edit system.conf file.
sudo nano /etc/systemd/system.conf
Sudo Nano /Etc/Systemd/System.conf
  1. Add/uncomment/edit the following lines.
Sudo Nano /Etc/Systemd/System.conf Add Watchdog

Here is what these lines mean:

RuntimeWatchdogSec is the time that if the watchdog does not receive a signal in 10 seconds (watchdog timer), then it will consider the system frozen and reboot the pi. Ensure that this value is not greater than 15 seconds because Raspberry Pi will not be able to handle that, and the system will be in a continuous reboot loop. 

RebootWatchdogSec is the time that the watchdog will wait for a clean reboot. In this case, it will wait 10 minutes for a clean reboot; if the system does not reboot in that time, it will forcefully reboot the system.

Refer to the systemd-system.conf man page here

  1. Reboot the system using
sudo reboot
Sudo Reboot After System.conf Edit
  1. Now you can check whether the watchdog is running using 
dmesg | grep watchdog 
Dmesg | Grep Watchdog 
  1. If you are confident enough, you can manually create a kernel panic and check if the system reboots automatically. 
sudo su
echo 1 > /proc/sys/kernel/sysrq
echo "c" > /proc/sysrq-trigger
Create Kernel Panic Using Shell Command

Steps to Enable Load-Based Watchdog on Raspberry Pi

Now, if you want to go a step further and reboot your Raspberry Pi if it exceeds a specific load limit, you can do it using the Linux watchdog software available in the repository. 

Also read: How to Automatically Spin Down or Sleep HDD on Raspberry Pi

You can also integrate Hardware watchdog with this, but we prefer to use system.conf for that, and rightfully so. We recommend you do that, too!

  1. Install watchdog using the following command
sudo apt install watchdog
Sudo Apt Install Watchdog
  1. Open watchdog.conf using 
sudo nano /etc/watchdog.conf
Sudo Nano /Etc/Watchdog.conf
  1. Add/uncomment/edit the following line and save the file
max-load-1 = 12

Here, max-load-1 means if the 1-minute system load exceeds 12, then the system will reboot. Adjust this value as per requirements, but don’t try to keep it too low. Similarly, you can set max-load-5 and max-load-15 for 5 & 15 minutes of system load, respectively. Refer to the watchdog.conf man page here

  1. To enable the watchdog service, use the commands below
sudo systemctl enable watchdog
sudo systemctl start watchdog
Enable Watchdog Service
  1. Check the status of the watchdog using the following command
sudo systemctl status watchdog
Check Watchdog Service Status

So that was all; now you have a 24*7 running Raspberry Pi with auto-healing & self-recovery mechanisms to make your remote server experience seamless. 

“As an Amazon Associate & Affiliate Partners of several other brands we earn from qualifying purchases.” [Read More Here]

Leave a Comment

Please enter your comment!
Please enter your name here

By submitting the above comment form, you agree to our Privacy Policy and agree with the storage and handling of your data by this website.

Stay Connected

Subscribe to our Newsletter

Stay updated with all the latest news, offers and special announcements.

By signing up, you agree to our Privacy Policy and agree with the storage and handling of your data by this website.

Latest Posts

How to Monitor the Raspberry Pi’s Temperature

Raspberry Pi has gotten very powerful over the years, especially with the launch of Raspberry Pi 5. Putting things in perspective, currently, we are...

How to Uninstall a Package on Ubuntu (GUI & Terminal)

Ubuntu is one of the widely used Linux distributions. Many people, including me, started my Linux journey with it. It is one of the...

Understanding YouTube Monetization | What Do You Need to Know

Do you want your captivating content on YouTube to turn into real income? Then look no further! We have just the guide here for...

What Are The Advantages & Disadvantages Of Android TV

In today's technological age, Android TVs are all the rage. Based on the Android ecosystem, Android TVs offer a range of features and functionalities....

How to Disable IPv6 Permanently on Raspberry Pi

On paper, IPv6 is a great thing because there are 340 undecillion addresses available, and it can be allocated uniquely to every device connected...

All Time Favorites

11 Best WiFi Hacking Apps for Android (2023)

Today, almost anywhere we go, we are surrounded by tons of WiFi Networks, whether at our homes, hotels, workplaces, or even public places like...

17 Best Illegal Android Apps Not on Play Store (2023)

Google Play Store has the most extensive collection of apps for Android. Almost everyone downloads apps from the Play Store. However, certain genuinely excellent...

15 Best Game Hacking Apps for Android (2023)

Are you a mobile gamer? If you are, then you've come to the right place. This post will tell you about the Best Game...

How to Download Torrent Files with IDM (Torrent to IDM)

Hey guys, you may all have heard about different ways to download torrent files. And some of you may also have downloaded them using...

11 Best Download Managers for Windows

The Internet is a vast resource of a lot of things which includes music, videos, movies and a lot of other things. The ability...