-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample.py
64 lines (50 loc) · 1.81 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
import time
import signal
from functools import partial
from pywatchdog import Watchdog
DEFAULT_DEVICE = "/dev/watchdog"
# Disable buffering so that messages are instantly displayed in systemctl status.
printnow = partial(print, flush=True)
class SimpleWatchdogDaemon:
"""A very basic daemon that notifies the watchdog every second."""
def __init__(self, device=DEFAULT_DEVICE):
self._device = device
self._running = False
self._set_handlers()
def _set_handlers(self):
signal.signal(signal.SIGINT, self.handler)
signal.signal(signal.SIGTERM, self.handler) # Sent by systemd (KillSignal).
def handler(self, signum, frame):
self._running = False
printnow(f"Received {signal.Signals(signum).name}. Stopping...")
def run(self):
with Watchdog(self._device) as wdt:
printnow("pywatchdog daemon started successfully.")
self._running = True
while self._running:
wdt.keep_alive()
time.sleep(1)
if __name__ == "__main__":
import argparse
import errno
import sys
parser = argparse.ArgumentParser()
parser.add_argument(
"device", nargs="?", default=DEFAULT_DEVICE, help="default: %(default)s"
)
args = parser.parse_args()
daemon = SimpleWatchdogDaemon(args.device)
try:
daemon.run()
except FileNotFoundError:
sys.exit(f"{args.device} not found")
except PermissionError as e:
if e.errno == errno.EACCES:
sys.exit(f"Can't access {args.device}. Try running as root")
raise
except OSError as e:
if e.errno == errno.EBUSY:
sys.exit(f"{args.device} is already used by a different process")
raise
printnow("pywatchdog daemon stopped successfully.")