Python
script termination
keyboard interrupt
process control
duplicate question

How do I abort the execution of a Python script?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Python provides several ways to abort script execution: sys.exit() for normal termination, exit() and quit() for interactive sessions, os._exit() for immediate process termination without cleanup, and KeyboardInterrupt (Ctrl+C) for manual interruption. The right choice depends on whether you need cleanup code to run, whether you are in a main script or a thread, and whether you want to signal success or failure to the calling process.

Method 1: sys.exit()

sys.exit() is the standard way to terminate a Python script. It raises SystemExit, which can be caught by try/except blocks, allowing cleanup code to run:

python
1import sys
2
3def process_data(filename):
4    if not filename.endswith('.csv'):
5        print("Error: Only CSV files are supported")
6        sys.exit(1)  # Exit with error code 1
7
8    print(f"Processing {filename}...")
9    # ... processing logic ...
10    sys.exit(0)  # Exit successfully
11
12process_data("data.txt")
13# Output: Error: Only CSV files are supported
14# Script exits with code 1

Exit codes: 0 means success, any non-zero value means failure. The calling process (shell, CI system) reads this code:

bash
python script.py
echo $?  # Prints the exit code (0 or 1)

sys.exit() and finally Blocks

sys.exit() raises SystemExit, so finally blocks and context managers still execute:

python
1import sys
2
3try:
4    print("Starting work")
5    sys.exit(1)
6except SystemExit as e:
7    print(f"Caught SystemExit with code {e.code}")
8    raise  # Re-raise to actually exit
9finally:
10    print("Cleanup runs regardless")

Method 2: Keyboard Interrupt (Ctrl+C)

Press Ctrl+C in the terminal to send a SIGINT signal, which raises KeyboardInterrupt:

python
1import time
2
3try:
4    while True:
5        print("Working...")
6        time.sleep(1)
7except KeyboardInterrupt:
8    print("\nInterrupted by user. Cleaning up...")
9    # Save progress, close files, etc.

Handle KeyboardInterrupt to perform graceful cleanup before exiting.

Method 3: exit() and quit()

exit() and quit() are convenience functions for the interactive interpreter. They work in scripts but are not recommended for production code:

python
1# Interactive Python session
2>>> exit()     # Exits the interpreter
3>>> quit()     # Same as exit()
4
5# In scripts — works but not recommended
6if error_condition:
7    exit(1)  # Prefer sys.exit(1) instead

exit() and quit() are defined in the site module, which may not be loaded in some embedded Python environments. sys.exit() is always available.

Method 4: os._exit()

os._exit() terminates the process immediately without calling cleanup handlers, flushing buffers, or running finally blocks:

python
1import os
2
3# Immediate termination — no cleanup
4os._exit(1)
5
6# Nothing below this line executes
7print("This never prints")

Use os._exit() only in child processes after os.fork() to avoid running the parent's cleanup code:

python
1import os
2
3pid = os.fork()
4if pid == 0:
5    # Child process
6    print("Child doing work")
7    os._exit(0)  # Exit child without parent cleanup
8else:
9    # Parent process
10    os.waitpid(pid, 0)
11    print("Parent continues")

Method 5: raise SystemExit

Equivalent to sys.exit() without importing sys:

python
1# These are equivalent:
2import sys
3sys.exit(1)
4
5raise SystemExit(1)
6
7raise SystemExit("Fatal error occurred")
8# Prints the message to stderr and exits with code 1

Method 6: Signals

Send signals to the process from outside:

bash
1# From another terminal
2kill -SIGTERM <pid>    # Graceful termination
3kill -SIGKILL <pid>    # Force kill (cannot be caught)
4kill -SIGINT <pid>     # Same as Ctrl+C

Handle signals in Python:

python
1import signal
2import sys
3
4def handle_sigterm(signum, frame):
5    print("Received SIGTERM, shutting down gracefully...")
6    # Cleanup code here
7    sys.exit(0)
8
9signal.signal(signal.SIGTERM, handle_sigterm)
10
11# Long-running process
12while True:
13    pass  # Terminates gracefully on SIGTERM

Exiting from Threads

sys.exit() in a thread only terminates that thread, not the entire process:

python
1import threading
2import os
3import sys
4
5def worker():
6    print("Thread running")
7    sys.exit(1)  # Only exits this thread!
8    # Use os._exit(1) to kill the entire process from a thread
9
10t = threading.Thread(target=worker)
11t.start()
12t.join()
13print("Main thread continues!")  # This still prints

To terminate the entire process from a thread, use os._exit():

python
def worker():
    print("Thread detected fatal error")
    os._exit(1)  # Kills the entire process immediately

Comparison Table

MethodCleanup RunsUse Case
sys.exit(code)YesStandard script termination
exit() / quit()YesInteractive sessions only
os._exit(code)NoChild processes after fork
raise SystemExitYesSame as sys.exit()
Ctrl+CIf caughtManual interruption
SIGKILLNoForce kill unresponsive process

Common Pitfalls

  • Using exit() in production scripts: exit() and quit() depend on the site module, which may not be available in all environments. Use sys.exit() for reliable termination.
  • Catching SystemExit without re-raising: A bare except Exception does not catch SystemExit (it inherits from BaseException), but except BaseException or except: does. If caught, re-raise it to allow the script to exit.
  • Using sys.exit() in threads: sys.exit() only terminates the calling thread, not the process. Use os._exit() or set a shared flag to signal the main thread to exit.
  • Forgetting cleanup on os._exit(): os._exit() skips finally blocks, atexit handlers, and buffer flushes. Data written to files may be lost. Only use it in forked child processes.
  • Not handling KeyboardInterrupt in long-running scripts: Without a try/except KeyboardInterrupt block, Ctrl+C prints an ugly traceback. Catch it to perform graceful shutdown and print a clean message.

Summary

  • Use sys.exit(0) for successful termination and sys.exit(1) for errors
  • Use Ctrl+C (KeyboardInterrupt) to manually abort a running script
  • sys.exit() raises SystemExit, allowing finally blocks and cleanup to run
  • Use os._exit() only in forked child processes where cleanup must be skipped
  • Handle signal.SIGTERM for graceful shutdown in long-running services

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.