# KeyboardInterrupt (script stopped with Ctrl+C) / ^C traceback

**Code:** `KeyboardInterrupt` · **Category:** Python · **Seen on:** Python

The script was interrupted (Ctrl+C, SIGINT, or a container stop), producing a KeyboardInterrupt traceback.

## Meaning

Not a bug in itself — but long-running scripts should catch it to clean up (close files, flush data) and exit gracefully. Docker sends SIGTERM first, which Python doesn’t handle by default.

## Common causes

- Manual Ctrl+C
- Process manager or container stop
- Infinite loop that had to be interrupted

## Quick fix

1. Wrap the main loop in try/except KeyboardInterrupt to clean up
2. Handle SIGTERM with the signal module in containers
3. Fix infinite loops if the interrupt was needed

## Detailed fix by platform

### Python

1. 

```python
import signal, sys
signal.signal(signal.SIGTERM, lambda *_: sys.exit(0))
```


## How to diagnose

1. **Trigger** — Who interrupted it?
2. **Cleanup** — Was data left half-written?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/python/keyboardinterrupt-in-script)
