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.
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.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment