Problem Statement
Explain common Linux signals (SIGTERM, SIGKILL, SIGHUP, SIGINT, SIGSTOP). When should you use each signal?
Explanation
SIGTERM (15) is the default termination signal sent by kill command without arguments. It requests graceful shutdown, allowing the process to clean up resources, close files, save state, and exit cleanly. Well-behaved applications catch SIGTERM, perform cleanup, and exit. Use SIGTERM first when stopping processes to avoid data corruption or resource leaks.
SIGKILL (9) forcefully terminates processes immediately without allowing cleanup. The process cannot catch or ignore SIGKILL - the kernel terminates it immediately. Use SIGKILL only as last resort when SIGTERM fails or for unresponsive processes. Warning: SIGKILL can cause data loss, incomplete transactions, or resource leaks since processes can't clean up.
SIGHUP (1) originally meant hangup (terminal disconnection) but is commonly used to tell daemons to reload configuration without restarting. Many services like nginx, apache, and sshd reload config on SIGHUP: kill -HUP PID or killall -HUP nginx. This allows updating configuration without downtime.
SIGINT (2) is sent by Ctrl+C, requesting interrupt. Like SIGTERM, it allows cleanup but is typically sent interactively. SIGSTOP (19) suspends process execution (can't be caught), while SIGCONT (18) resumes. Use kill -STOP PID to pause process, kill -CONT PID to resume. Understanding signals is essential for proper process management, service restarts, and troubleshooting stuck processes.
Practice Sets
This question appears in the following practice sets:
