How to Send and Catch SIGTERM in Bash and Python

SIGTERM is a signal that is sent to a process to request its termination. Unlike the SIGKILL signal, which is used to kill a process immediately, the SIGTERM signal can be caught and handled by the process. This allows the process to perform some cleanup operations before exiting gracefully.
In this article, you will learn how to send and catch SIGTERM signals in Bash and Python scripts. You will also learn how to use the trap command in Bash and the signal module in Python to register signal handlers and perform custom actions when a SIGTERM signal is received.
Sending SIGTERM Signals in Bash and Python
To send a SIGTERM signal to a process, you need to know its process ID (PID). You can use the ps command in Bash to list the processes running on your system and their PIDs. For example, the following command will show the PID and the command name of all the processes that contain the word “python” in their name:
ps -ef | grep python
The output might look something like this:
user 1234 1 0 10:15 pts/0 00:00:00 python3 script1.py
user 2345 1 0 10:16 pts/1 00:00:00 python3 script2.py
user 3456 1234 0 10:17 pts/0 00:00:00 /usr/bin/python3 /usr/bin/pip3 install requests
user 4567 2345 0 10:18 pts/1 00:00:00 /usr/bin/python3 /usr/bin/pip3 install numpy
user 5678 2345 0 10:19 pts/1 00:00:00 /usr/bin/python3 /usr/bin/pip3 install pandas
The first column shows the user name, the second column shows the PID, the third column shows the parent PID, the fourth column shows the CPU usage, the fifth column shows the terminal name, the sixth column shows the start time, the seventh column shows the elapsed time, and the eighth column shows the command name.
To send a SIGTERM signal to a process, you can use the kill command in Bash with the PID as an argument. For example, the following command will send a SIGTERM signal to the process with PID 1234:
kill 1234
Alternatively, you can use the pkill command in Bash with the command name as an argument. For example, the following command will send a SIGTERM signal to all the processes that contain the word “python” in their name:
pkill python
You can also send a SIGTERM signal to a process from another Python script. To do this, you need to import the os module and use the os.kill function with the PID and the signal number as arguments. The signal number for SIGTERM is 15. For example, the following Python script will send a SIGTERM signal to the process with PID 1234:
Python
import os
os.kill(1234, 15)
Catching SIGTERM Signals in Bash and Python
To catch a SIGTERM signal in a Bash script, you need to use the trap command with the signal name and the action to perform as arguments. The action can be a command, a function, or a string. For example, the following Bash script will print a message and exit when it receives a SIGTERM signal:
#!/bin/bash
trap 'echo "Received SIGTERM signal. Exiting..."; exit' SIGTERMecho "Running script..."
while true; do
sleep 1
done
To catch a SIGTERM signal in a Python script, you need to import the signal module and use the signal.signal function with the signal name and the handler function as arguments. The handler function is a function that takes two parameters: the signal number and the current stack frame. The handler function can perform any custom actions when the signal is received. For example, the following Python script will print a message and exit when it receives a SIGTERM signal:
Python
import signal
import sys
def handler(signum, frame):
print("Received SIGTERM signal. Exiting...")
sys.exit()signal.signal(signal.SIGTERM, handler)print("Running script...")
while True:
pass
Conclusion
In this article, you learned how to send and catch SIGTERM signals in Bash and Python scripts. You also learned how to use the trap command in Bash and the signal module in Python to register signal handlers and perform custom actions when a SIGTERM signal is received.