Watchdog Sample

The watchdog is a very powerful concept. Wikipedia says:

watchdog timer (sometimes called a computer operating properly or COP timer, or simply a watchdog) is an electronic timer that is used to detect and recover from computer malfunctions. During normal operation, the computer regularly resets the watchdog timer to prevent it from elapsing, or “timing out”. If, due to a hardware fault or program error, the computer fails to reset the watchdog, the timer will elapse and generate a timeout signal. The timeout signal is used to initiate corrective action or actions. The corrective actions typically include placing the computer system in a safe state and restoring normal system operation.

The sample below is very basic. You will need to adjust the duration and location of the watchdog for your application.

package com.integ.watchdogsample;

import com.integpg.system.JANOS;
import com.integpg.system.Watchdog;

public class WatchdogSampleMain {

    public static void main(String[] args) throws InterruptedException {
        JANOS.syslog("WatchdogSample started");

        // create a watchdog that will reboot the jnior if the watchdog is not fed once every 5 seconds
        Watchdog watchdog = new Watchdog("WatchdogSample");
        JANOS.syslog("WatchdogSample activated");

        // loop for 30 seconds.  we will feed the watchdog in this loop.  when the loop stops the watchdog will timeout and reboot the jnior.
        long loopExpiration = System.currentTimeMillis() + 30000;
        while (loopExpiration > System.currentTimeMillis()) {
            // feed the watchdog
            // sleep for a second

        // now that the loop is finished.  the unit will reboot soon.
        JANOS.syslog("WatchdogSample loop finished");


This is what we see in the log after the unit has come back up.

CODE: Select All

01/15/19 08:37:36.361, FTP/ uploaded /flash/WatchdogSample.jar [115.3 kbps]
01/15/19 08:37:44.081, WatchdogSample started
01/15/19 08:37:44.203, WatchdogSample activated
01/15/19 08:38:14.307, WatchdogSample loop finished
01/15/19 08:38:18.451, ** Assertion: WatchdogSample watchdog triggered reboot (Line 1278)
01/15/19 08:38:18.483, ** Terminating: System
01/15/19 08:38:20.971, ** Reboot on assertion: WatchdogSample watchdog triggered reboot (Line 1278)
01/15/19 08:38:20.997, -- JANOS 410 v1.7.1 initialized (POR: 1546)

Here are the options that are available to choose from when the watchdog expires.



This is the default action. Expiration of the watchdog timer causes the unit to reboot.



This action indicates that a watchdog timer expiration is to be handled by the application itself. The system takes no action.



When the watchdog timer expires the system will terminate the application.



This interrupts the current application (similar to Ctrl-C interruption) when the watchdog timer expires.



When the watchdog timer expires this will interrupt the current application and restart it.



When the watchdog timer expires a WM_WATCHDOG (0x11) message will be sent through the system message pump. The content contains the text associated with the watchdog.



Watchdog timer expiration will notify the first thread in the application that is waiting  for notification using waitOnWatchdogNotify().

This post was written by Kevin Cloutier on July 31, 2018 at 11:14 am


Categorized in: