Reputation: 501
I run python script inside a cygwin shell but I'm not able to intercept ctrl + c command.
This is my python script:
#!/cygdrive/c/python27/python.exe -u
import signal
import sys
def signal_handler(sig, frame):
print('You pressed Ctrl+C!')
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGBREAK, signal_handler)
print('Press Ctrl+C')
input()
Also using a try/catch method still not working
#!/cygdrive/c/python27/python.exe -u
try:
input()
except KeyboardInterrupt:
print('Interrupted')
but non of these handler works with ctrl + c command. The Cygwin version is 1.7.25(0.270/5/3) and I'm using python 2.7. What's wrong?
Upvotes: 5
Views: 449
Reputation: 1322
I have the same problem in the MSYS environment (https://www.msys2.org/) in its mingw64 terminal. I was finally able to fix the issue using the winpty
tool:
winpty python3 my_python_script.py
This way signal handlers work as expected. I'm just not sure if the winpty
tool is available in your cygwin distribution as well.
Upvotes: 1