import signal
import sys
def handle_sigterm(signum, frame):
"""
Signal handler for graceful shutdown.
Triggered when the process receives SIGTERM or SIGINT.
"""
print("Received shutdown signal, cleaning up...")
# Attempt to stop running browser processes gracefully.
# You can extend this list with any other browser names you use.
for b in ("firefox","edge"):
try:
stop_function(b) # user-defined cleanup function
except Exception:
# Ignore any errors during cleanup to ensure shutdown continues
pass
# Exit the process cleanly
sys.exit(0)
# Register the handler for termination (SIGTERM) and interrupt (SIGINT / Ctrl+C)
signal.signal(signal.SIGTERM, handle_sigterm)
signal.signal(signal.SIGINT, handle_sigterm)