User Tools

Site Tools


Hardware Watchdog

While older versions of OpenWrt used the watchdog daemon from busybox, all new versions implement the watchdog daemon via procd, which is the init process (PID1). Therefore on modern OpenWrt, you will never see the watchdog process running.

Supported Hardware

Most embedded devices will have their hardware watchdog enabled in the default kernel configuration of their target, which means it will be built-in (no kmod to install, it's always available).

For x86 hardware you have some kmods to choose, kmod-it87-wdt kmod-sp5100_tco which are for AMD hardware, kmod-itco-wdt for Intel hardware and kmod-w83627hf-wdt that can work for both.

There are also USB-based watchdogs, which will be discussed below in their own sub-section because they are not handled by procd in any way.

The procd watchdog code always uses the primary watchdog device /dev/watchdog. You can configure what watchdog that is (ie GSC Watchdog or SoC watchdog) by disabling all but the desired watchdog in the kernel configuration, or not installing watchdog kmods for x86 architecture.

Controlling the Watchdog

You can see the current configuration of the watchdog service via ubus:

If there is a watchdog available:

root@OpenWrt:/# ubus call system watchdog
        "status": "running",
        "timeout": 30,
        "frequency": 5

If no watchdog is available:

root@OpenWrt:/# ubus call system watchdog
        "status": "offline",
        "timeout": 30,
        "frequency": 5

While there is no uci configuration available for these options you could change them in an rc script such as rc.local if you wish:

change to 60s timeout

ubus call system watchdog '{ "timeout": 60 }'   

change to 1s frequency

ubus call system watchdog '{ "frequency": 1 }'  

To stop the service:

ubus call system watchdog '{ "stop": true }'   # watchdog will cause a reset after it expires

Manually control hardware watchdog

If you just stop procd from tickling the hardware watchdog you still can't manually tickle watchdog:

root@OpenWrt:~# ubus call system watchdog '{"stop": true}'  
        "status": "stopped",
        "timeout": 30,
        "frequency": 5,
        "magicclose": false
root@OpenWrt:~# echo 1 > /dev/watchdog  
-ash: can't create /dev/watchdog: Resource busy

But if you enable magicclose feature then you have control:

root@OpenWrt:~# ubus call system watchdog '{"magicclose": true}'  
        "status": "running",
        "timeout": 30,
        "frequency": 5,
        "magicclose": true

root@OpenWrt:~# ubus call system watchdog '{"stop": true}'  
        "status": "offline",
        "timeout": 0,
        "frequency": 0,
        "magicclose": true
root@OpenWrt:~# echo 1 > /dev/watchdog  

USB Watchdogs

USB-based watchdogs are basically a microcontroller over a USB-serial dongle, that will close/open contacts when they trigger. These contacts are usually on a header that can be connected to the Power and Reset switch of the board, or to a relay wired to interrupt the power supply to the device.

These are some photos of the device I am using:

This kind of task is trivially easy to also do with an Arduino or Attiny board and some relays.

Some USB-based watchdogs are not using a serial dongle, but show up as USB HID devices and I don't know what is their protocol.

This is a script that will generate a Procd service to control one of such USB-serial watchdogs

#watchdog control script for USB-serial based watchdogs
# the command to run the watchdog 160 seconds is
#echo -en '\x10' > /dev/ttyUSB0
#the "x10" is 16 in hexadecimal
# and will tell the watchdog to run for 160 seconds, setting a higher (or lower) time is possible,
# just write a different hexadecimal number, and adjust the number of seconds in the sleep command below
# DO NOT use a too small time though or the system will not have enough time to fully reboot before the watchdog triggers again.
#first we are detecting what usb serial we need to use, to avoid sending commands to the wrong device, like LTE modems
#the device I used appears like this in dmesg when I connect it
#[11705.304457] usb 4-1: new full-speed USB device number 2 using uhci_hcd
#[11705.538687] ch341 4-1:1.0: ch341-uart converter detected
#[11705.549626] usb 4-1: ch341-uart converter now attached to ttyUSB0
#so I filter for ch341 serial chips
usb_serial=$(dmesg | grep ch341 | grep tty | awk '{print $NF}' )
if [ "$usb_serial" = '' ]; then
	echo no ch341 serial dongle detected, no watchdog available
	exit 1
case "$1" in
touch /tmp/stop_usb_watchdog
exit 0
cat > /etc/init.d/usb_watchdog <<'END_OF_MESSAGE'
#!/bin/sh /etc/rc.common
start_service() {
    procd_set_param command /bin/sh "/etc/usb_watchdog"
    #if process dies sooner than respawn_threshold, it is considered crashed and after 5 retries the service is stopped
    procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
        /etc/usb_watchdog stop
chmod +x /etc/init.d/usb_watchdog
cat > /lib/upgrade/keep.d/usb_watchdog <<'END_OF_MESSAGE'
/etc/init.d/usb_watchdog enable
/etc/init.d/usb_watchdog start
grep -q "usb_watchdog" /etc/rc.local
if [ "$?" = 1 ]; then
	sed -i '/exit 0/i /etc/init.d/usb_watchdog enable' /etc/rc.local
exit 0
rm /lib/upgrade/keep.d/usb_watchdog
rm /etc/usb_watchdog
rm /etc/init.d/usb_watchdog
sed -i 's|/etc/init.d/usb_watchdog enable||g' /etc/rc.local
exit 0
echo starting usb watchdog service
while [ ! -f /tmp/stop_usb_watchdog ]; do
	echo -en '\x10' > /dev/"$usb_serial"
	sleep 30
rm /tmp/stop_usb_watchdog
echo stopping usb watchdog service

To install it, write the following commands in your device's command line interface:

wget --no-check-certificate -O /etc/usb_watchdog
chmod +x /etc/usb_watchdog
/etc/usb_watchdog install

You can then control it with

service usb_watchdog

as normal for a procd service

This script also writes the system configuration to survive a system upgrade, so you shouldn't need to reinstall it when you upgrade.

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
docs/guide-user/hardware/watchdog.txt · Last modified: 2019/07/14 00:19 by bobafetthotmail