Signals
Signals are short asynchronous notifications sent to processes, used to stop, pause, resume, or ask them to reload. Knowing the common signals is essential for managing programs.
What signals are
A signal is a numbered notification delivered to a process by the kernel, another process, or the terminal. When a signal arrives, the process takes the default action (terminate, ignore, stop, continue, or terminate with a core dump), runs a handler function it registered, or ignores it if it chose to. Signals interrupt whatever the process was doing.
Common signals
SIGINT (2) is sent by Ctrl+C and politely interrupts. SIGTERM (15) is the default signal of kill, asking a process to terminate cleanly. SIGKILL (9) kills immediately and cannot be caught or ignored. SIGHUP (1) originally meant the terminal hung up and is often used to make daemons reload configuration. SIGSTOP and SIGTSTP (Ctrl+Z) pause a process; SIGCONT resumes it.
Error signals
Some signals report program errors. SIGSEGV indicates an invalid memory access, a segmentation fault; SIGFPE an arithmetic error such as integer division by zero; SIGILL an illegal instruction; and SIGABRT is raised by abort(), for example after a failed assertion. By default these terminate the process and may produce a core dump for debugging. SIGPIPE is sent when writing to a pipe whose reader has gone.
Sending signals
kill PID sends SIGTERM; kill -9 PID or kill -KILL PID sends SIGKILL; kill -HUP PID sends SIGHUP. pkill and killall send signals by process name, such as pkill -f 'python server.py'. Ordinary users may signal only their own processes; root may signal any. kill -0 PID sends no signal but checks whether the process exists and can be signalled.
Handling signals
Programs install handlers with sigaction() to clean up on SIGTERM, such as saving data, removing temporary files, and closing connections, before exiting. Because handlers interrupt normal execution, they should do minimal work, often just setting a flag the main loop checks. In shell scripts, trap 'cleanup' TERM INT runs a function when those signals arrive.
Graceful shutdown
The right way to stop a service is to send SIGTERM and give it time to finish, escalating to SIGKILL only if it does not exit. systemd does this when stopping services, waiting a configurable timeout, 90 seconds by default, before sending SIGKILL. Using kill -9 first risks corrupted files, lost data, and leftover lock files.
Signals and process groups
Processes belong to process groups and sessions. A shell puts each pipeline in its own process group, so Ctrl+C sends SIGINT to every process in the foreground job at once. Daemons detach from terminals by starting new sessions so terminal signals do not affect them.
Worked example
A web server with PID 2201 ignores configuration changes until restarted. Running kill -HUP 2201 tells servers such as nginx to reload configuration without dropping connections. Later, to stop it, kill 2201 sends SIGTERM; nginx finishes current requests and exits. If a different program hangs and ignores SIGTERM, kill -9 forces it to end, accepting that it cannot clean up.
Key takeaways
Signals are asynchronous notifications with default actions such as terminate, stop, or ignore, unless a process installs a handler. SIGINT (Ctrl+C), SIGTERM (kill's default), and SIGKILL (9, uncatchable) end processes; SIGHUP often means reload; SIGSTOP, SIGTSTP, and SIGCONT pause and resume. SIGSEGV, SIGFPE, SIGILL, and SIGABRT report errors and may dump core; SIGPIPE reports broken pipes. kill, pkill, and killall send signals, limited by ownership. Handlers and trap enable clean shutdown; send SIGTERM first and SIGKILL only as a last resort, as systemd does after a timeout. Ctrl+C signals the whole foreground process group.
Flashcard
Asynchronous notification to a process
Check your understanding
Why should administrators send SIGTERM before resorting to SIGKILL?
