Neha Verma Neha Verma - 1 year ago 108
Python Question

Python- How to check if program gets aborted by user while running?

If I am running a python program on linux terminal and i abort it manually by pressing ctrl+c, how can i make my program do something when this event occurs.

something like:

if sys.exit():
print "you chose to end the program"

Answer Source

You can write a signal handling function

import signal,sys
def signal_handling(signum,frame):
    print "you chose to end the program"

while True:

pressing Ctrl+c sends a SIGINT interrupt which would output:

you chose to end the program

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download