Categories: TutorialsUbuntu

How to Capture Linux Signal in Python

Many times developers need to terminate scripts that are running, using Ctrl+C or other signals. But this is often processed by OS, and not the actual script. But sometimes you may need the script to be able to receive and handle system signals from within the script itself. In this article, we will learn how to capture Linux signal in Python.

How to Capture Linux Signal in Python

Python provides an inbuilt library called signal that allows you to easily capture and work with signals. Let us create a sample python script to capture Linux signal.

$ vi test.py

Add the following lines to set the execution environment and import required libraries.

#!/usr/bin/env python
import signal
import sys

Next, we define the signal handler.

def signal_handler(sig, frame):
    print('You pressed Ctrl+C!')
    sys.exit(0)

Next, we register this signal handler using signal.signal(). We specify two arguments in it – the signal that will call the handler, and the name of the handler.

signal.signal(signal.SIGINT, signal_handler)
print('Press Ctrl+C')
signal.pause()

We use the signal.pause() function to cause the process to sleep until it receives a signal. Save and close the file.

Putting it all together.

#!/usr/bin/env python
import signal
import sys

def signal_handler(sig, frame):
    print('You pressed Ctrl+C!')
    sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)
print('Press Ctrl+C')
signal.pause()

Make the file executable.

$ sudo chmod +x test.py

Run the file with the following command.

$ python test.py

In this article, we have learnt how to capture Linux signal in python script.

Also read:

How to Send Signal from Python
How to Clear Canvas for Redrawing in JS
How to Use Decimal Step Value for Range in Python
How to Get Browser Viewport Dimensions in JS
How to Auto Resize TextArea to Fit Text

The post How to Capture Linux Signal in Python appeared first on Fedingo.

Ubuntu Server Admin

Recent Posts

How to use Ubuntu on Windows

Why run Ubuntu on Windows? It’s about getting the best of both worlds. Many organizations…

18 hours ago

Fixes available for CVE-2026-31431 (Copy Fail) Linux Kernel Local Privilege Escalation Vulnerability

A local privilege escalation (LPE) vulnerability affecting the Linux kernel has been publicly disclosed on…

3 days ago

How to Install Bitwarden on Ubuntu 26.04

Bitwarden is an open-source password manager application designed to store and encrypt all of its…

4 days ago

Run NVIDIA Nemotron 3 Nano Omni locally in a single command

Today, NVIDIA introduced the NVIDIA Nemotron™ 3 Nano Omni, a highly-efficient multimodal model designed to…

6 days ago

Why Web Engineering is great

Like many software engineers, one of my first software development experiences started with creating my…

1 week ago

Ubuntu 16.04 LTS has reached the end of standard Expanded Security Maintenance with Ubuntu Pro. Here are your options.

Ubuntu 16.04 LTS (Xenial Xerus) reached the end of its five-year Expanded Security Maintenance (ESM)…

1 week ago