Watchdog
The watchdog feature, provided through the watchdog module, exists as a safeguard. If there are critical operations that “must” happen periodically, or else the system will be irretrievably broken, an application can request that a “watchdog” be established. If the application threads do not service their watchdog within the promised interval, the entire system reboots. You can change or, if necessary, delete the intervals for these software watchdogs. Using a software watchdog exists as a measure of last resort. Appropriate error detection and handling with Python scripts is certainly recommended.
The following sample program demonstrates the watchdog feature.
Note The highlighted numbers in the sample code correspond to the items in the Program Notes, below.
import watchdog (1) import time w=watchdog.Watchdog('test',20) (2) for x in xrange(1,6): (3) print "Step ", x (4) time.sleep(10.0) (5) w.heartbeat() (6) print "Step just before the end..." (7) time.sleep(60.0) (8) print "Step after the end." (9)
Program notes
- The watchdog module includes the Watchdog class needed by the program.
- Create a watchdog object named “test” that will expire in 20 seconds.
- Loop five times (1-5).
- Indicate our iteration...
- ... sleeping less than the timeout on each iteration, but more time than the timeout in total.
- Reset the watchdog timer to 20 seconds each iteration, allowing all of the loops to complete.
- Indicate that small loops are complete.
- Sleep for an interval much longer than the timeout.
- This print statement never executes, because the system will reboot when the watchdog timeout expires.