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.

Sponsored
class="wp-block-preformatted">#!/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.

Sponsored
#!/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

Building optimized LLM chatbots with Canonical and NVIDIA

The landscape of generative AI is rapidly evolving, and building robust, scalable large language model…

6 hours ago

Unlocking Edge AI: a collaborative reference architecture with NVIDIA

The world of edge AI is rapidly transforming how devices and data centers work together.…

6 hours ago

How to Install and Use Zig Programming Language on Ubuntu or Debian Linux

In this article, we will see how to install and use zig programming language on…

9 hours ago

Linux Sed Tutorial: Learn Text Editing with Syntax and Examples

This article was adapted from its original version on NixCraft. Sed is an acronym for…

15 hours ago

How to Fix VMware’s “Could not open /dev/vmmon” Error on Ubuntu

You’ve recently installed VMware Workstation on your Ubuntu system and encountered the frustrating “Could not…

23 hours ago

How to Fix Ubuntu 404 Errors While Fetching Dependencies

Have you ever found yourself staring at a terminal full of 404 errors while trying…

23 hours ago