Hardware Watchdog on Linux: /dev/watchdog and systemd WatchdogSec

Check whether a Linux board exposes a watchdog device, learn how /dev/watchdog behaves, and make systemd restart a service that stops reporting in.

Embedded Published 4 min read

Contents

A watchdog is a timer that resets the machine, or restarts a program, unless something keeps telling it that all is well. On Linux there are two layers: the hardware watchdog exposed as /dev/watchdog, and the per-service watchdog built into systemd. This article shows how to see what your board offers and how to configure a service so systemd restarts it when it stops reporting.

How /dev/watchdog works

According to the kernel documentation, a userspace program must periodically tell the driver it is still alive. If those notifications stop, the hardware resets the system after the timeout.

The basic rules from the kernel’s watchdog API document:

  • Opening /dev/watchdog starts the watchdog.
  • Writing any data to the device, or calling the WDIOC_KEEPALIVE ioctl, pings it.
  • By default, closing the device disables the watchdog, unless the driver supports “Magic Close”. With Magic Close, the watchdog stays armed on close unless the character V was written just before.
  • If the kernel is built with CONFIG_WATCHDOG_NOWAYOUT, or the driver’s nowayout parameter is set, the watchdog cannot be disabled once started.
  • WDIOC_SETTIMEOUT sets the timeout in seconds on drivers that support it, and the driver reports the value it actually applied, which can differ because of hardware granularity. WDIOC_GETTIMEOUT reads it back.

Magic Close support depends on the driver, so check your driver’s behavior before relying on the watchdog staying armed after the device is closed.

Check whether your board has a watchdog

Look for watchdog device nodes and for kernel messages mentioning a watchdog (systemd’s default device is /dev/watchdog0):

ls -l /dev/watchdog*
ls /sys/class/watchdog/
dmesg | grep -i watchdog

If you build your own image, see Buildroot: Build a Minimal Linux Image.

Avoid opening /dev/watchdog by hand on a real machine to test it: opening it starts the timer, and the system may reset.

Let systemd feed the hardware watchdog

systemd can ping the hardware watchdog itself. In /etc/systemd/system.conf (or a drop-in under system.conf.d/), set:

[Manager]
RuntimeWatchdogSec=30

Per the systemd documentation, a non-zero value makes the manager use the watchdog device (/dev/watchdog0 by default, changeable with WatchdogDevice=) and reboot the system if it is not contacted within the timeout. The manager pings it at least once in half the timeout. The default is 0, which means off.

Make a service restart when it stops reporting

For a single service, use WatchdogSec= in its unit. The service must send WATCHDOG=1 through sd_notify() more often than the timeout. If the gap grows larger, systemd marks the service failed and terminates it with SIGABRT (or the signal in WatchdogSignal=). Add Restart=on-watchdog to restart only in that case, or on-failure to cover other failures too.

Two details from systemd.service(5): systemd passes the configured timeout to the process in the WATCHDOG_USEC environment variable, so a daemon can enable pinging automatically; and NotifyAccess= must allow the notification socket, otherwise it is implicitly set to main.

Here is a unit that uses the systemd-notify command as a stand-in for a real daemon. The systemd-notify page documents sending VARIABLE=VALUE assignments, though it does not list WATCHDOG=1 specifically, so treat this as a demonstration of the mechanism:

If systemd is not installed (for example in a minimal container), install it first:

✓ tested · debian,ubuntu
apt-get update && apt-get install -y systemd
✓ tested · fedora
dnf install -y systemd
✓ tested · debian,ubuntu,fedora,arch
mkdir -p /etc/systemd/system
cat > /etc/systemd/system/demo-watchdog.service <<'EOF'
[Unit]
Description=Demo service with a systemd watchdog

[Service]
ExecStart=/bin/sh -c 'while true; do systemd-notify WATCHDOG=1; sleep 5; done'
WatchdogSec=20
NotifyAccess=all
Restart=on-watchdog

[Install]
WantedBy=multi-user.target
EOF

NotifyAccess=all is used because systemd-notify runs as a child process of the shell, not as the main process. Now check the unit syntax with systemd-analyze verify:

✓ tested · debian,ubuntu,fedora,arch
systemd-analyze verify /etc/systemd/system/demo-watchdog.service

On a real system, start the unit with systemctl start demo-watchdog and watch it with systemctl status, as covered in systemctl: Start, Stop, Enable and Inspect Services. To see a watchdog timeout, stop the pings (for example, change the loop to sleep longer than WatchdogSec=) and read the result in the journal; journalctl covers the filtering.

Notes for production

  • TimeoutAbortSec= controls how long a service has to finish after the watchdog SIGABRT (for example to write a core dump) before systemd sends SIGKILL.
  • RuntimeWatchdogSec= configures the hardware watchdog that systemd pings; WatchdogSec= is set per service in its unit.

Sources

  1. The Linux Watchdog driver API (kernel documentation)
  2. systemd.service(5): WatchdogSec=, NotifyAccess=, Restart=
  3. systemd-system.conf(5): RuntimeWatchdogSec=, WatchdogDevice=
  4. systemd-notify(1)

#Watchdog #Systemd #Embedded #Reliability #WatchdogSec

This article was drafted with AI assistance, fact-checked against the sources listed above and reviewed by a human editor before publication. Spotted an error? See our corrections policy.