Skip to content

Instantly share code, notes, and snippets.

@coseos
Created September 20, 2022 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coseos/875682dd492e92d99ac50b3720a447da to your computer and use it in GitHub Desktop.
Save coseos/875682dd492e92d99ac50b3720a447da to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# This is a template on how to add a CTRL-C handler in your Python script
#
# With this handler, you can include a proper cleanup before the script is terminated.
#
from signal import signal, SIGINT
from sys import exit
def handler(signal_received, frame):
# Handle any cleanup here
print('SIGINT or CTRL-C detected. Exiting gracefully')
exit(0)
if __name__ == '__main__':
# Tell Python to run the handler() function when SIGINT is recieved
signal(SIGINT, handler)
print('Running. Press CTRL-C to exit.')
while True:
# Do nothing and hog CPU forever until SIGINT received.
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment