Python Scripts: handel sigint signal gracefully
This commit is contained in:
parent
e51b96a901
commit
f0435961cb
1 changed files with 17 additions and 0 deletions
|
@ -4,6 +4,22 @@ import time
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
from datetime import datetime
|
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):
|
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():
|
def main():
|
||||||
|
signal.signal(signal.SIGINT, handle_sigint)
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
description="Test used drive with SMART, badblocks, and fio."
|
description="Test used drive with SMART, badblocks, and fio."
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Reference in a new issue