Linux Watchdog: How It Reboots a Frozen System
In this article
The Linux watchdog is a hardware or software mechanism that automatically reboots your system when it becomes unresponsive (frozen). It works by expecting a periodic "kick" (write) from the kernel or a userspace daemon; if the kick doesn't arrive within a set timeout, the watchdog triggers a hardware reset. This guide explains how to enable and configure the software watchdog (using the watchdog daemon and the softdog kernel module) so your system recovers from hangs without manual intervention. You'll need root access and a basic comfort with the command line.
What You'll Need
- A Linux system (most distributions work; examples use Debian/Ubuntu commands).
- Root or sudo access.
- An active internet connection to install packages (if not already installed).
- Optional: a hardware watchdog device (like Intel's TCO timer) if your motherboard supports it; the steps below work with the software watchdog for testing.
Step-by-Step
- Check if a watchdog device already exists. Run
ls /dev/watchdog*. If you see/dev/watchdogor/dev/watchdog0, you have a watchdog device (likely hardware). If not, you'll use the software watchdog module. - Load the software watchdog kernel module. Run
sudo modprobe softdog. This creates a/dev/watchdogdevice (if not already present). To make it load automatically at boot, addsoftdogto/etc/modules(on Debian/Ubuntu) or create a file in/etc/modules-load.d/with the linesoftdog. - Install the watchdog daemon. Run
sudo apt install watchdog(Debian/Ubuntu) orsudo dnf install watchdog(Fedora). This provides thewatchdoguserspace daemon that kicks the device. - Configure the daemon. Edit
/etc/watchdog.confwith sudo. Uncomment or add the linewatchdog-device = /dev/watchdog. Also setinterval = 10(seconds between kicks) andtimeout = 60(seconds without a kick before reset). The timeout must be longer than the interval; a common ratio is 1:6. Save the file. - Enable and start the daemon. Run
sudo systemctl enable watchdogandsudo systemctl start watchdog. Check its status withsudo systemctl status watchdog– it should show as active (running). - Verify the watchdog is active. Run
sudo wdctl(if available) to see the device, timeout, and current status. You can also check kernel logs withdmesg | grep watchdogto see that the softdog module registered. - Test the watchdog (optional but recommended). To safely test, you can stop the daemon and then intentionally freeze the system. For a non-destructive test, use a kernel panic trigger: run
echo c > /proc/sysrq-trigger(as root) – this causes a kernel panic, and the watchdog should reset the system within the timeout. Warning: This will reboot your system and may lose unsaved work. Only do this when you're ready for a reboot. - Ensure the daemon starts at boot. If you used systemctl enable, it's set. For non-systemd systems, ensure the watchdog service is started via init scripts.
If your system freezes completely (even the kernel), the hardware watchdog (if present) will still reset. The software watchdog (softdog) relies on the kernel being alive enough to handle the timer interrupt; if the kernel is completely dead, softdog may not fire. For full protection, use a hardware watchdog if your motherboard has one – the steps are the same, but the device is already present.
Common Mistakes
- Setting timeout too short: If the timeout is shorter than the interval, the watchdog may reset the system during normal operation. Always keep timeout at least 3-4 times the interval.
- Not loading the module at boot: If you don't add
softdogto modules, the device won't exist after reboot, and the daemon will fail. - Forgetting to enable the service: Starting it manually works only until the next reboot; use
systemctl enable. - Using the wrong device path: Some systems use
/dev/watchdog0instead of/dev/watchdog. Check withls /dev/watchdog*and update the config. - Ignoring log errors: If the daemon fails to start, check
journalctl -u watchdogfor errors like "cannot open watchdog device".
Tips
- Use a hardware watchdog when possible: Hardware watchdogs (like Intel TCO) are more reliable because they don't depend on the kernel's timer interrupt. Check
dmesg | grep -i watchdogto see if your hardware has one. - Adjust the timeout based on your system's boot time: The watchdog starts kicking after boot, so if your system takes a long time to boot, a short timeout might cause a reset loop. Set timeout to at least 60 seconds for most systems.
- Monitor the daemon: You can check the daemon's status periodically with
sudo systemctl status watchdogto ensure it's running. - Test in a VM first: If you're unsure, test in a virtual machine to avoid unexpected reboots on your main system.
- Keep the system clock reliable: The watchdog uses timeouts based on the system clock; if the clock jumps (e.g., NTP), it may cause false resets. Disable NTP if you experience issues.
Troubleshooting
If the watchdog does not reboot a frozen system:
- Check if the daemon is running:
sudo systemctl status watchdog. If it's not active, start it and check logs withjournalctl -u watchdogfor errors. - Verify the device exists:
ls /dev/watchdog*. If not, load the module withsudo modprobe softdogand add it to modules. - Test the device manually: Write to the device to see if it responds:
sudo timeout 10 dd if=/dev/zero of=/dev/watchdog– this should cause a reboot after the timeout (if the device is working). Warning: This will reboot your system immediately after 10 seconds. - Check kernel logs:
dmesg | grep -i watchdog– look for errors like "watchdog: device did not stop" or "softdog: unexpected close". - If using a hardware watchdog, ensure it's enabled in BIOS: Some motherboards have a watchdog timer that must be enabled in the firmware settings. Check your motherboard manual.
- Try increasing the timeout: If the system freezes but the watchdog doesn't fire, the timeout might be too long. Reduce it to 30 seconds and test again.
- If the system reboots too often (false positives): Increase the timeout and interval, and ensure the daemon is not being killed by OOM or other issues. Check
journalctlfor OOM events.
Frequently Asked Questions
Does the software watchdog work if the kernel completely hangs?
No. The softdog module relies on the kernel's timer interrupt to fire. If the kernel is completely dead (e.g., hardware failure), softdog won't trigger. A hardware watchdog is needed for that.
Can I use the watchdog to recover from a userspace freeze?
Yes, that's the main use case. If a userspace process hangs and the system becomes unresponsive, the daemon stops kicking, and the watchdog resets the system.
Will the watchdog cause data loss?
Yes, a watchdog reset is like pulling the power plug – unsaved data in RAM is lost. The system does not go through a clean shutdown.
How do I disable the watchdog after enabling it?
Stop and disable the daemon with sudo systemctl stop watchdog and sudo systemctl disable watchdog. Also remove softdog from /etc/modules or the modules-load.d file, and reboot to unload the module.
Can I have multiple watchdog devices?
Yes, but the daemon typically uses only one. You can specify a different device in the config. Having multiple doesn't provide extra protection unless they're independent.
What is the difference between softdog and a hardware watchdog?
Softdog is a kernel module that simulates a watchdog using a timer; it depends on the CPU and kernel. A hardware watchdog is a separate chip that counts down independently; it resets the system even if the CPU is completely stuck.