Process Management & System MonitoringLesson 4.2
Killing and signaling processes in Linux
kill command, SIGTERM SIGKILL SIGHUP SIGINT, kill -9 vs graceful shutdown, pkill killall, signals in scripts, trap command
Signals Are Messages Sent to Processes
A signal is a notification sent to a process. The process can choose to handle or ignore most signals. SIGKILL is the exception — it cannot be caught or ignored, and the kernel terminates the process immediately.
Common Signals
SIGTERM (15) requests graceful shutdown — the process cleans up and exits. This is the default for kill. SIGKILL (9) forces immediate termination. SIGHUP (1) traditionally reloads config without restarting. SIGINT (2) is what Ctrl+C sends.
# Graceful shutdown (SIGTERM is default)
kill 1234
# Force kill (use only when graceful fails)
kill -9 1234
# Kill by name (kills all matching processes)
pkill nginx
# Kill with SIGHUP to reload config
kill -HUP $(pgrep nginx)
# Kill all processes by name (be careful)
killall python3trap — Catch Signals in Scripts
Use trap to run cleanup code when your script receives a signal or exits.
#!/bin/bash
cleanup() {
echo "Cleaning up temp files..."
rm -f /tmp/myapp_*
exit 0
}
# Run cleanup on Ctrl+C or normal exit
trap cleanup SIGINT SIGTERM EXIT
echo "Working..."
sleep 100Always add a trap for cleanup in scripts that create temp files or hold locks.
