| by Arround The Web | No comments

How to Send and Catch SIGTERM in Bash and Python

In Linux, processes communicate with each other using signals. The signals act as software interrupts that notify the programs of various events or requests. For instance, when a program is running, you can send a kill signal to exit the program. The signal that is sent may trigger different actions such as stopping an execution, terminating the program, resuming execution, etc.

When working with Bash and Python, you can send the SIGTERM signal to terminate the running process softly. This post covers about sending and catching SIGTERM in Bash and Python.

What Is SIGTERM

Unix systems have three categories of signals: system, device, and user-defined signals. Each signal has an integer value. You can execute the signal by specifying its name or its integer value.

SIGTERM is a signal with an integer value of 15. It is executed when you want to terminate a running process softly. The following is the syntax to use SIGTERM in Bash:

kill -SIGTERM <PID>

or

kill -15 <PID>

You can check all the available signals using the following command:

kill -l

How to Send and Catch SIGTERM in Bash and Python

You may want to send and catch SIGTERM in Bash and Python in different instances. When you execute your Bash or Python program, you can execute the SIGTERM signal to kill the program. We have different examples to understand how you send and catch SIGTERM in Bash and Python.

1. Using a Python Script

When using the Python 1.4 and latest versions, you can utilize the signal library to send and catch SIGTERM. Import the library in your program to define how your program should capture and react to different signals. The signal library lets you create a signal handler to report the integer of the received signal. You can then register the captured signal and get the information about the current process such as its PID. Let’s give an example.

We have a Python script that catches the integer of the sent signal. Moreover, it catches the PID of the current process.

In the following image, we execute the Python script and have a while loop that executes every few seconds. On another terminal, we can send the SIGTERM signal to kill the process by specifying the PID that we get from executing the Python script:

Once we send the SIGTERM signal, we notice that the script stops executing. That’s because it captures the signal integer. In this case, we execute the kill -15 for the SIGTERM signal. You can also send the SIGTERM kill signal using the SIGTERM keyword instead of its integer value. We still get the same result.

You can also execute your Python job using the following syntax:

python3 <script/job> &

Unlike the first method, when we send the SIGTERM using this option, we can see that the job ID is printed out when the job starts executing. After we kill it, we get the “done” status. The SIGTERM can be sent by specifying its name or its integer.

2. Using a Bash Script

You can use the “trap” command to catch a signal when executing a Bash script. In this example, we added the “trap” command to catch the SIGTERM signal. The script is a “for” loop to execute the “date” command a thousand times if no SIGTERM signal is sent.

When executing the Bash script, you can send the SIGTERM signal by pressing the Ctrl + Z keyboard keys. The “trap” command captures the transmitted signal, and the “for” loop will quit executing. You will get an output which confirms that the running job has received a SIGTERM signal and has stopped.

That’s how you send and catch SIGTERM in Bash.

Conclusion

You can send different signals to achieve different goals when executing a job or program. SIGTERM is sent to stop a program softly. For Python, you need a signal handler to catch a signal, and you can send the SIGTERM using the “kill” command. You can use the “trap” command and keyboard keys on Bash to capture and send the SIGTERM signal. This post presented different examples on how to achieve that in detail.

Share Button

Source: linuxhint.com

Leave a Reply