From 7418ebb9e7c0d9ee0867fe8d93548322e08fedc5 Mon Sep 17 00:00:00 2001 From: lowar Date: Sun, 27 Oct 2024 23:22:46 +0100 Subject: [PATCH] Add a SIGTERM handler to gracefully shutdown process --- SunGather/sungather.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/SunGather/sungather.py b/SunGather/sungather.py index f196170..8fa0439 100644 --- a/SunGather/sungather.py +++ b/SunGather/sungather.py @@ -10,6 +10,7 @@ import getopt import yaml import time +import signal def main(): configfilename = 'config.yaml' @@ -145,6 +146,8 @@ def main(): scan_interval = config_inverter.get('scan_interval') + signal.signal(signal.SIGTERM, handle_sigterm) + # Core polling loop while True: loop_start = time.perf_counter() @@ -181,6 +184,11 @@ def main(): logging.info(f'Next scrape in {int(scan_interval - process_time)} secs') time.sleep(scan_interval - process_time) +def handle_sigterm(signum, frame): + print("Received SIGTERM, shutting down gracefully...") + # Perform any cleanup here + exit(0) + logging.basicConfig( format='%(asctime)s %(levelname)-8s %(message)s', level=logging.DEBUG,