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

Terminal
$kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM ...

The ones you'll use most:

SignalNumberTriggerEffect
SIGHUP1Terminal closedHangup
SIGINT2Ctrl+CInterrupt
SIGQUIT3Ctrl+\Quit + core dump
SIGKILL9kill -9Force terminate
SIGTERM15killPolite terminate
SIGSTOP19Ctrl+ZStop (pause)
SIGCONT18fg/bgContinue

Sending Signals

Terminal
$kill -SIGTERM 1234
$kill -15 1234
(same thing)
$kill 1234
(same thing - SIGTERM is default)

Three ways to specify the same signal.

SIGHUP - Reload Config

Many daemons reload their config when they receive SIGHUP:

Terminal
$kill -HUP $(pgrep nginx)
(nginx reloads config)

Graceful Reload

Instead of restarting a service:

hljs bash
kill -HUP $(pgrep nginx)

This reloads config without dropping connections.

SIGKILL vs SIGTERM

SIGTERM (15)SIGKILL (9)
Can be caughtYesNo
Can be ignoredYesNo
Cleanup possibleYesNo
Always worksUsuallyAlways

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:

Terminal
$# Many apps use these for debugging
$kill -USR1 1234
(app might dump state/toggle debug)

Check application documentation for what these do.

Trapping Signals in Scripts

Scripts can catch signals:

hljs bash
#!/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

Terminal
$sleep 1000 &
[1] 1234
$kill -STOP 1234
$jobs
[1]+ Stopped sleep 1000
$kill -CONT 1234
$jobs
[1]+ Running sleep 1000 &
$kill 1234
$jobs
[1]+ Terminated sleep 1000

STOP pauses, CONT resumes, TERM terminates.

Knowledge Check

Why should you try SIGTERM before SIGKILL?

Quick Reference

SignalSend WithCommon Use
SIGHUPkill -1Reload config
SIGINTCtrl+CStop interactively
SIGKILLkill -9Force terminate
SIGTERMkillPolite terminate
SIGSTOPkill -STOPPause process
SIGCONTkill -CONTResume 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 -l to see all signals

Next: keeping processes running after logout.