Python Scripts: handel sigint signal gracefully

This commit is contained in:
Aroy-Art 2025-04-12 13:27:20 +02:00
parent e51b96a901
commit f0435961cb
Signed by: Aroy
GPG key ID: 583642324A1D2070

View file

@ -4,6 +4,22 @@ import time
import sys
import os
from datetime import datetime
import signal
interrupted_once = False
interrupted_time = 0
def handle_sigint(signum, frame):
global interrupted_once, interrupted_time
now = time.time()
if interrupted_once and (now - interrupted_time < 5):
print("\n[!] Second interrupt received. Exiting now.")
sys.exit(1)
else:
interrupted_once = True
interrupted_time = now
print("\n[!] Interrupt received. Press Ctrl+C again within 5 seconds to exit.")
def run_cmd(cmd, log_file, check=True, capture_output=True):
@ -106,6 +122,7 @@ def run_fio(device, rw_mode, log_file, bs="4k", runtime=30):
def main():
signal.signal(signal.SIGINT, handle_sigint)
parser = argparse.ArgumentParser(
description="Test used drive with SMART, badblocks, and fio."
)