Skip to main content

Signal

A signal is an asynchronous notification sent to a process by the kernel, another process, or the user. A process can handle it with custom code, ignore it, or accept the default action, which is often termination.

Understanding Linux Signals

What Is a Signal in Linux?

Signals tell a process something happened without sending data: shut down, reload config, pause. Pressing Ctrl+C sends SIGINT, kill 1234 sends SIGTERM, and systemctl reload triggers a reload that, for services like nginx, is implemented by sending SIGHUP. Graceful shutdowns and zero-downtime reloads depend on them.

How Signal Delivery Works

The kernel marks the signal as pending for the target process. When that process next runs, the kernel checks pending signals and either runs the process's handler or applies the default action.

sequenceDiagram participant S as Shell participant K as Kernel participant P as Process 1234 S->>K: kill -TERM 1234 K->>P: mark SIGTERM pending P->>P: run handler and clean up P-->>K: exit

The Most Important Signals

Numbers below are for common architectures such as x86 and ARM. A few differ on other architectures, so use names in scripts and check kill -l if unsure.

TEXT
SIGHUP 1 Reload config (terminal hang-up originally)
SIGINT 2 Interrupt, Ctrl+C. Can be caught
SIGKILL 9 Immediate kill. Cannot be caught, blocked, or ignored
SIGUSR1 10 Application-defined; some daemons use it to reopen logs
SIGUSR2 12 Application-defined
SIGTERM 15 Polite terminate. Default of kill. Can be caught
SIGCHLD 17 A child changed state
SIGCONT 18 Resume a stopped process
SIGSTOP 19 Pause. Cannot be caught or ignored

Why SIGTERM Comes Before SIGKILL

SIGTERM lets a process finish in-flight requests, close database connections, flush buffers, and remove lock files. SIGKILL gives it none of that, which can leave locks behind and drop requests. A sound shutdown sequence is: send SIGTERM, wait (for example 30 seconds), then SIGKILL only if it is still running. Kubernetes follows this pattern with its termination grace period.

Practical Signal Commands

Bash
kill 1234 # SIGTERM
kill -HUP 1234 # reload
kill -9 1234 # last resort
kill -0 1234 # test existence and permission, sends nothing
kill -l # list signals
pkill -f 'python3 worker.py'
killall nginx
# Handle signals in a script
tmpfile=$(mktemp)
trap 'rm -f "$tmpfile"' EXIT
trap 'echo "SIGTERM caught"; exit 0' TERM

Troubleshooting Signals

Symptom Command What to Look For
kill has no effect ps -o stat= -p PID D state, waiting on I/O
Reload does nothing Check app docs No SIGHUP handler
Ignores SIGTERM Escalate after timeout Handler swallows it
Zombies ps -eo stat,pid,ppid Parent not reaping
Tip

kill -0 PID is the safest liveness check. Exit code 0 means the process exists and you may signal it.

Common Mistake

In containers, running a shell-form CMD so the shell becomes PID 1 and swallows SIGTERM. Use exec form or exec app in the entrypoint so the app receives it.

Security

Processes of the same user can signal each other, and root can signal anything. Dedicated service accounts limit what a compromised service can disturb.

Frequently Asked Questions

What is the practical difference between SIGTERM and SIGKILL?

SIGTERM can be caught, so the app can shut down gracefully. SIGKILL is enforced by the kernel and cannot be caught, so there is no cleanup. Always try SIGTERM first.

Why does my container ignore docker stop?

If the app runs as PID 1 without a SIGTERM handler, or a shell wrapper sits in front of it, the signal is never acted on and Docker sends SIGKILL after the timeout. Use an init such as tini or `docker run --init`, and exec form commands.

How do I reload a service without restarting it?

If the service implements it, `systemctl reload name` or `kill -HUP PID` makes it re-read its config without dropping connections. Not every program handles SIGHUP.

Found a mistake, an outdated step or a broken link?