Signals
Signals are how the kernel and processes communicate. When you press Ctrl+C, a signal is sent. When a process crashes, a signal is sent.
Common Signals
The ones you'll use most:
| Signal | Number | Trigger | Effect |
|---|---|---|---|
| SIGHUP | 1 | Terminal closed | Hangup |
| SIGINT | 2 | Ctrl+C | Interrupt |
| SIGQUIT | 3 | Ctrl+\ | Quit + core dump |
| SIGKILL | 9 | kill -9 | Force terminate |
| SIGTERM | 15 | kill | Polite terminate |
| SIGSTOP | 19 | Ctrl+Z | Stop (pause) |
| SIGCONT | 18 | fg/bg | Continue |
Sending Signals
Three ways to specify the same signal.
SIGHUP - Reload Config
Many daemons reload their config when they receive SIGHUP:
Graceful Reload
Instead of restarting a service:
kill -HUP $(pgrep nginx)
This reloads config without dropping connections.
SIGKILL vs SIGTERM
| SIGTERM (15) | SIGKILL (9) | |
|---|---|---|
| Can be caught | Yes | No |
| Can be ignored | Yes | No |
| Cleanup possible | Yes | No |
| Always works | Usually | Always |
SIGTERM asks politely. Process can:
- Catch it and cleanup
- Ignore it completely
SIGKILL can't be caught or ignored. It's handled by the kernel.
User-Defined Signals
Programs can handle SIGUSR1 and SIGUSR2 for custom actions:
Check application documentation for what these do.
Trapping Signals in Scripts
Scripts can catch signals:
#!/bin/bash
cleanup() {
echo "Caught signal, cleaning up..."
rm -f /tmp/lockfile
exit 0
}
trap cleanup SIGTERM SIGINT
# Main script...
This ensures cleanup even if killed.
Signal Flow Example
STOP pauses, CONT resumes, TERM terminates.
Why should you try SIGTERM before SIGKILL?
Quick Reference
| Signal | Send With | Common Use |
|---|---|---|
| SIGHUP | kill -1 | Reload config |
| SIGINT | Ctrl+C | Stop interactively |
| SIGKILL | kill -9 | Force terminate |
| SIGTERM | kill | Polite terminate |
| SIGSTOP | kill -STOP | Pause process |
| SIGCONT | kill -CONT | Resume process |
Key Takeaways
- Signals are inter-process communication
- SIGTERM (15) is polite, SIGKILL (9) is forced
- SIGHUP often reloads config without restart
- Scripts can trap signals for cleanup
- Use
kill -lto see all signals
Next: keeping processes running after logout.