CUSTOMISED
Expert-led training for your team
Dismiss
How to Send Ctrl-C to Interrupt and Stop a Python Script

3 November 2023

How to Send Ctrl-C to Interrupt and Stop a Python Script

Python is a popular high-level programming language used for scripting, web development, data analysis, machine learning, and more. When running Python scripts, you may need to abruptly stop or interrupt long-running processes. This can be done using the Ctrl-C keyboard shortcut, which sends a SIGINT signal, triggering a KeyboardInterrupt exception in Python.

This article covers everything you need to know about handling Ctrl-C interrupts and exceptions gracefully in your Python code. The material is taken from JBI Trainings python training. We'll explain what happens when Ctrl-C is sent, how to simulate it programmatically, and how to catch and handle the resulting KeyboardInterrupt.

Understanding Ctrl-C Keyboard Interrupts in Python

Pressing Ctrl-C while a Python script is running sends a SIGINT (signal interrupt) to the Python interpreter, telling it to immediately stop whatever it's doing.

This SIGINT signal effectively pauses and interrupts the execution of the program. It raises a KeyboardInterrupt exception within the Python script.

What is the SIGINT Signal?

The SIGINT signal is a form of interrupt triggered by the Ctrl-C key combination. It is one of several signals that the Python interpreter handles to manage various events like user interrupts, system exits, and runtime errors.

SIGINT stands for "signal interrupt". It is defined in the OS level signal module and connected to the Ctrl-C shortcut.

The KeyboardInterrupt Exception

When SIGINT is received, Python will raise a KeyboardInterrupt exception. This immediately stops the normal flow of the program.

The KeyboardInterrupt inherits from Python's BaseException so it is not handled like a typical exception. The default behaviour is to display a stack trace and exit the program entirely.

Python's Signal Handling Process

Python has built-in signal handling to deal with events like SIGINT from Ctrl-C. Here is what happens:

  1. Ctrl-C is pressed, sending a SIGINT signal to the Python process.
  2. The signal module handles the SIGINT, triggering a KeyboardInterrupt.
  3. This exception pauses execution and displays a stack trace.
  4. By default, the Python script will exit unless the exception is caught.

Proper handling of SIGINT and KeyboardInterrupt allows scripts to gracefully shutdown instead of abrupt termination.

Sending a Ctrl-C Signal to Interrupt a Python Script

The SIGINT signal that Ctrl-C sends can also be simulated programmatically. The os.kill() method allows you to send a signal to a specified process ID.

Here is an example script that sends SIGINT to itself:


import os
import time

print("My PID is:", os.getpid())

time.sleep(5) 

print("Sending SIGINT to self...")
os.kill(os.getpid(), signal.SIGINT)

This Python script prints its own process ID, pauses for 5 seconds, then uses os.kill() to deliver a SIGINT to itself.

Running this code has the same effect as pressing Ctrl+C - the script will be interrupted mid-execution, showing a KeyboardInterrupt trace.

Using os.kill() to Send SIGINT

The os.kill(pid, sig) method sends the signal sig to the process pid. To simulate Ctrl-C, we pass the SIGINT signal to our own process ID.

Some key points:

  • os.getpid() gets the current process ID to target
  • signal.SIGINT specifies the interrupt signal
  • An external process ID can also be used with os.kill

This technique provides a way to programmatically generate a KeyboardInterrupt anywhere in your code.

Catching and Handling the KeyboardInterrupt Exception

Simply sending SIGINT to a Python script will abruptly stop it with a stack trace. To handle Ctrl-C gracefully, we need to catch and handle the resulting KeyboardInterrupt.

The try/except construct is used to catch and deal with exceptions in Python:


import signal
import sys

try:
  # Main program code here  
except KeyboardInterrupt:
  print("Ctrl-C pressed!")
  sys.exit(0)

This will detect a KeyboardInterrupt, print a message, and exit the program cleanly when Ctrl+C is pressed.

Using try/except to Catch KeyboardInterrupt

  • Enclose the main code in a try block to catch exceptions.
  • Detect KeyboardInterrupt in the except block.
  • Additional cleanup or shutdown code can be run before exiting.
  • This prevents abrupt termination on Ctrl-C.

With proper handling, SIGINT can be used for graceful program termination. The script shuts down on its own terms instead of a forced crash.

Common Examples of Ctrl-C Handling in Python

Handling Ctrl-C allows you to build robust scripts that react appropriately on keyboard interrupt. Here are some common examples:

1. Exiting Long-Running Loops

Ctrl-C is often used to exit an infinite while loop:


try:
  while True:
    # Do some long running task  
except KeyboardInterrupt:
  print("Exiting loop!")

This allows the user to manually stop the loop with Ctrl-C when needed.

2. Terminating Server Processes

Server scripts like web apps need to gracefully shutdown on SIGINT:


import signal

def shutdown(sig, frame):
  print("Server shutting down...")
  sys.exit(0)

signal.signal(signal.SIGINT, shutdown)

# Start web server

Registering the SIGINT signal handler allows the server to exit cleanly.

3. Stopping Recursive Functions

Recursive functions can be halted mid-execution:


import signal

def recursive_func():
  try:
    recursive_func()
  except KeyboardInterrupt:
    print("Stopping recursion!")  

The KeyboardInterrupt exception ends the recursion when Ctrl+C is sent by the user.

Proper handling of keyboard interrupts allows the creation of scripts that are responsive and resilient.

Summary and Conclusion

  • Ctrl-C sends a SIGINT signal that raises a KeyboardInterrupt in Python
  • Use os.kill() and signal.SIGINT to simulate Ctrl-C programmatically
  • Catch and handle KeyboardInterrupt with try/except blocks
  • This allows graceful handling of Ctrl-C instead of sudden termination
  • Common use cases include stopping loops, exiting servers, and interrupting recursive functions

With this knowledge, you can create Python programs that safely handle keyboard interrupts for an improved user experience. The same principles can be applied across languages and systems to make applications more robust.

Next Steps:

  • Learn about other Python signals like SIGTERM
  • Build a shutdown handler function for your Python scripts
  • Explore alternate approaches like signal handlers
  • Handle Ctrl-C in child processes and threaded code
  • Keep up to date with tips and how to guides by accessing the JBI Training blog, click here for our last article on Python and Machine Learning 

I hope this guide gave you a good understanding of handling Ctrl-C keyboard interrupts in Python. Let me know if you have any other questions!

Frequently Asked Questions About Ctrl-C in Python

Here are some common FAQs about sending Ctrl-C signals and handling KeyboardInterrupt exceptions in Python:

Q: What exactly happens when I press Ctrl-C during a Python program?

Pressing Ctrl-C sends a SIGINT signal to the Python interpreter, causing it to raise a KeyboardInterrupt exception and interrupt the execution of your program. This stops the program and prints a stack trace.

Q: How can I simulate Ctrl-C programmatically in Python?

You can use os.kill(os.getpid(), signal.SIGINT) to send a SIGINT signal to the current Python process. This will raise KeyboardInterrupt just like a real Ctrl-C press.

Q: Why does my Python program crash instead of exiting cleanly when I press Ctrl-C?

By default, Python displays the KeyboardInterrupt stack trace and crashes the program. To prevent this, you need to catch and handle the exception properly using try/except blocks.

Q: What's the best way to gracefully exit a Python program when Ctrl-C is pressed?

Catch the KeyboardInterrupt in a try/except block. Print any shutdown messages, clean up resources, then use sys.exit() to cleanly stop the program.

Q: Can I use Ctrl-C handling to stop an infinite loop in Python?

Yes, placing the loop inside a try/except block will allow the user to stop the loop by pressing Ctrl-C and catching the KeyboardInterrupt.

Q: How do I stop a recursive function using Ctrl-C in Python?

Recursion can be halted by checking for KeyboardInterrupt inside the recursive function with try/except blocks.

Q: What are some real-world uses for handling Ctrl-C in Python scripts?

Handling Ctrl-C allows you to gracefully stop servers, end long-running processes, exit loops, and more. This is useful for building robust, production-ready scripts.

Q: Where can I learn more about signal handling in Python?

Check the Python documentation on signals, the signal module, and KeyboardInterrupt exceptions. The Linux manual pages for SIGINT are also helpful for understanding low-level details.

Other Python Courses from JBI Training

JBI Training offers a wide range of Python courses beyond basic programming. Here are some additional training options to take your Python skills to the next level:

  • Python for Data Analysis - Learn key data analysis libraries like Pandas, Numpy, Matplotlib, and Seaborn. Analyze and visualize data in Python.
  • Python for Machine Learning - Dive into machine learning with Python. Cover algorithms like regression, classification, clustering, neural networks, and more.
  • Python for Web Development - Build web applications with Python frameworks like Django and Flask. Learn to create both front-end and back-end systems.
  • Advanced Python - Level up with advanced Python features like multithreading, regular expressions, Python debugging, networking, and low-level programming.
  • Python for Finance - Use Python for financial data analysis, trading algorithm development, backtesting strategies, modeling, risk analysis, and more.
  • Asynchronous Python - Explore async programming techniques with asyncio and aiohttp. Learn to build reactive, high-performance applications.

With an expert instructor and hands-on projects, JBI Training provides impactful Python courses for programmers of all skill levels. Learn more on their website.

CONTACT
+44 (0)20 8446 7555

[email protected]

SHARE

 

Copyright © 2023 JBI Training. All Rights Reserved.
JB International Training Ltd  -  Company Registration Number: 08458005
Registered Address: Wohl Enterprise Hub, 2B Redbourne Avenue, London, N3 2BS

Modern Slavery Statement & Corporate Policies | Terms & Conditions | Contact Us

POPULAR

Rust training course                                                                          React training course

Threat modelling training course   Python for data analysts training course

Power BI training course                                   Machine Learning training course

Spring Boot Microservices training course              Terraform training course

Kubernetes training course                                                            C++ training course

Power Automate training course                               Clean Code training course