Linux Kernel / Process Management Termination Signal
SIGTERM is a signal sent to a process requesting it to terminate gracefully. Unlike SIGKILL (Signal 9), SIGTERM can be caught and handled by the process, allowing it to perform cleanup tasks (e.g., save state, close connections, flush buffers) before exiting. If a process does not handle SIGTERM or fails to exit within a reasonable timeframe, it might subsequently be sent a SIGKILL.
Common Causes
- User manually terminating a process (e.g., `kill <pid>`, `pkill <name>`).
- System shutdown or reboot sequence, where init systems send SIGTERM to running processes.
- Container orchestration platforms (e.g., Kubernetes) stopping a pod or container.
- Service managers (e.g., systemd, supervisord) stopping a service.
- Parent process terminating child processes.
- Application deployment or scaling operations that require old instances to shut down.
How to Fix
1 Implement Signal Handler in Application Code
Modify the application code to catch the SIGTERM signal and execute a graceful shutdown routine. This typically involves saving data, releasing resources, closing network connections, and then exiting cleanly. This is crucial for data integrity and application resilience.
$ import signal
import sys
import time
import os
def handle_sigterm(signum, frame):
print(f'\nReceived SIGTERM ({signum}). Initiating graceful shutdown...')
# Perform cleanup tasks here
# e.g., save data, close database connections, flush logs
time.sleep(2) # Simulate cleanup work
print('Cleanup complete. Exiting.')
sys.exit(0)
signal.signal(signal.SIGTERM, handle_sigterm)
print('Application running. PID:', os.getpid())
print('Send SIGTERM (e.g., kill -15 ' + str(os.getpid()) + ') to test.')
try:
while True:
time.sleep(1)
print('Working...')
except KeyboardInterrupt:
print('\nCaught Ctrl+C (SIGINT). Exiting.')
sys.exit(0) 2 Configure Service Manager Grace Period
When running applications as services (e.g., with systemd) or in container orchestrators (e.g., Kubernetes), configure the appropriate termination grace period. This gives the application enough time to handle SIGTERM and shut down gracefully before a forceful SIGKILL is issued.
$ # For systemd service unit file (e.g., /etc/systemd/system/my-app.service):
[Service]
ExecStart=/usr/local/bin/my-app
TimeoutStopSec=30s # Give the app 30 seconds to shut down gracefully
KillMode=process # Ensure only the main process is signaled
# For Kubernetes Pod YAML:
# apiVersion: v1
# kind: Pod
# metadata:
# name: my-app-pod
# spec:
# containers:
# - name: my-app-container
# image: my-app:latest
# terminationGracePeriodSeconds: 30 # Give the container 30 seconds 3 Manually Send SIGTERM to a Process
To manually test or terminate a process gracefully, use the `kill` command. By default, `kill` sends SIGTERM (signal 15) if no signal is specified. You need the Process ID (PID) of the target process.
$ # Find the PID of your process (replace 'my_app_name' with actual name)
pgrep my_app_name
# Then send SIGTERM using the PID
kill <PID_OF_PROCESS>
# Alternatively, explicitly specify signal 15
kill -15 <PID_OF_PROCESS>