Skip to content

Instantly share code, notes, and snippets.

@luqmanrom
Created May 11, 2024 11:57
Show Gist options
  • Save luqmanrom/af2528e7aa933a2edebbd0c8a9e57da5 to your computer and use it in GitHub Desktop.
Save luqmanrom/af2528e7aa933a2edebbd0c8a9e57da5 to your computer and use it in GitHub Desktop.

Revisions

  1. luqmanrom created this gist May 11, 2024.
    29 changes: 29 additions & 0 deletions detect-signal.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    import signal
    import time
    import os

    def handle_sigterm(signum, frame):
    print("Received SIGTERM. Cleaning up...")
    print("Cleanup done. Exiting gracefully.")
    exit(0)


    def handle_sigint(signum, frame):
    print("Received SIGINT. Cleaning up...")
    print("Cleanup done. Exiting gracefully.")
    exit(0)


    signal.signal(signal.SIGTERM, handle_sigterm)
    signal.signal(signal.SIGINT, handle_sigint)
    print(f"Running process with PID: {os.getpid()}")
    print(
    f"SIGKILL: kill -9 {os.getpid()}")

    print(
    f"SIGTERM: kill -15 {os.getpid()}")
    try:
    while True:
    time.sleep(1)
    except KeyboardInterrupt:
    print("Process interrupted by user.")