I\'ve got a script that runs on a infinite loop and adds things to a database and does things that I can\'t just stop halfway through so I can\'t just press ctrl+C and stop
the below logic will help you do this,
import signal
import sys
import time
run = True
def signal_handler(signal, frame):
global run
print "exiting"
run = False
signal.signal(signal.SIGINT, signal_handler)
while run:
print "hi"
time.sleep(1)
# do anything
print "bye"
while running this, try pressing CTRL+C