I configured the hardware watchdog with the following python code. The watchdog started normally, but the timeout period set was invalid. At the same time, after the end of the program, the watchdog is not closed, but also cause the system to restart.
How do I use python to properly set the timeout and turn off the watchdog after the program ends?
thank you
code:
def enable_watchdog(device_path=‘/dev/watchdog’, timeout=15):
try:
# Open the hardware watchdog device file
watchdog = os.open(device_path, os.O_WRONLY)
# Set the timeout period of the hardware watchdog in seconds
timeout_command = f"timeout={timeout}"
os.write(watchdog, timeout_command.encode())
# Writes arbitrary data to enable hardware watchdog
os.write(watchdog, b'\0')
print(f"Hardware watchdog enabled. The system will be restarted if not refreshed within {timeout} seconds.")
while True:
# Refresh the hardware watchdog to prevent system restart
os.write(watchdog, b'\0')
time.sleep(timeout // 2) # The refresh frequency is half of the timeout period
except Exception as e:
print(f"Error: {e}")
finally:
if watchdog:
os.close(watchdog)