Skip to content

Instantly share code, notes, and snippets.

@nicktimko
Created December 23, 2018 01:51
Show Gist options
  • Save nicktimko/83c56d5467abeaab31e11600a3a7e74f to your computer and use it in GitHub Desktop.
Save nicktimko/83c56d5467abeaab31e11600a3a7e74f to your computer and use it in GitHub Desktop.
Playing with the RPi WDT
#!/usr/bin/env python3
"""
Why bother with a service that hits the watchdog? The watchdog package seems
kinda daft if you're worried about your service locking up and/or not doing
something it should be doing.
Maybe could use something like this in conjuction with the watchdog daemon,
but don't want both to reset the WDT or that would be near pointless. Ideally
on my service start I'd like to permanently put the WDT into a state where
it's active, which seems to be done by opening the `/dev` file. Python
"unfortunately" generally wants to clean up dangling file handles (doubly so
when using the context manager like below), so maybe there's a way to
intentionally leak it on startup? Another process?
## Demo
```
nick@lappy386:~$ ssh pi@192.168.1.127
pi@192.168.1.127's password:
Linux raspberrypi 4.14.79-v7+ #1159 SMP Sun Nov 4 17:50:20 GMT 2018 armv7l
...
pi@raspberrypi:~ $ sudo python3 watchtest.py
OK.
0...OK.
0...1...OK.
0...1...2...OK.
0...1...2...3...OK.
0...1...2...3...4...OK.
0...1...2...3...4...5...OK.
0...1...2...3...4...5...6...OK.
0...1...2...3...4...5...6...7...OK.
0...1...2...3...4...5...6...7...8...OK.
0...1...2...3...4...5...6...7...8...9...OK.
0...1...2...3...4...5...6...7...8...9...10...OK.
0...1...2...3...4...5...6...7...8...9...10...11...OK.
0...1...2...3...4...5...6...7...8...9...10...11...12...OK.
0...1...2...3...4...5...6...7...8...9...10...11...12...13...OK.
0...1...2...3...4...5...6...7...8...9...10...11...12...13...14...OK.
0...1...2...3...4...5...6...7...8...9...10...11...12...13...14...15...Write failed: Broken pipe
nick@lappy386:~$ # aaaand it's dead.
```
"""
import sys
import time
for x in range(30):
with open("/dev/watchdog", "w") as f:
f.write('x')
for n in range(x):
print('{}...'.format(n), end='')
sys.stdout.flush()
time.sleep(1)
print('OK.')
f.write('x')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment