Unix Signal
- In Turkish
- sinyal
- Pronunciation
- YOO-niks SIG-nul
In short
A Unix signal is a short asynchronous notification the kernel delivers to a process to report an event, such as a stop request, an error, or an expired timer.
What is a Unix signal?
A Unix signal is a simple form of communication on Unix-like systems such as Linux, macOS, and the BSDs. Each signal is a numbered event with a name: SIGINT is sent when you press Ctrl+C, SIGTERM politely asks a process to terminate, SIGKILL forces it to stop, SIGHUP reports that the terminal closed and is often used to reload configuration, SIGSEGV reports an invalid memory access, and SIGCHLD tells a parent that a child process has exited.
Signals can be sent by the kernel when something happens, by the terminal when you press certain keys, or by another process through the kill system call or command. When a signal arrives, the process's normal flow is interrupted and one of three things happens: the default action runs, such as terminating the process, the signal is ignored, or a custom handler function runs. SIGKILL and SIGSTOP can never be caught or ignored. Because a handler can run at almost any moment, it should do very little, such as setting a flag that the main program checks.
A signal is like a tap on the shoulder: the message is tiny, only which tap it was, but it makes someone stop what they are doing and respond. Signals matter for graceful shutdown. Container platforms send SIGTERM, wait for a grace period of 30 seconds by default in Kubernetes, and then send SIGKILL, so servers should catch SIGTERM, stop accepting new requests, finish the ones in flight, and exit cleanly.
Signals are often confused with hardware interrupts. An interrupt is delivered to the CPU and handled by the kernel, while a signal is delivered by the kernel to a user process, which is why signals are sometimes called software interrupts for processes. The kill command is also misleadingly named: it can send any signal and sends SIGTERM by default. Windows does not use Unix signals and has its own mechanisms, although its C runtime emulates a few of them.
Key takeaways
- A signal is a small, asynchronous notification delivered to a process.
- Common signals include
SIGINT,SIGTERM,SIGKILL, andSIGHUP. - A process can use the default action, ignore a signal, or run a handler.
SIGKILLandSIGSTOPcannot be caught or ignored.- Handling
SIGTERMenables graceful shutdown in containers and services.
Example
#!/usr/bin/env bash
# Run cleanup when the script gets SIGINT (Ctrl+C) or SIGTERM
cleanup() { echo "Cleaning up..."; rm -f /tmp/myjob.lock; exit 0; }
trap cleanup INT TERM
touch /tmp/myjob.lock
sleep 300 &
wait $! # waiting this way lets the trap run right away
# From another terminal:
# kill -TERM <pid> polite request to stop (the default signal)
# kill -KILL <pid> forced stop that cannot be caught or ignoredReaders ask
What is the difference between SIGTERM and SIGKILL?
SIGTERM asks a process to stop and gives it a chance to clean up, and it can be caught or ignored. SIGKILL makes the kernel stop the process immediately, with no chance to clean up.
What happens when I press Ctrl+C in a terminal?
The terminal sends SIGINT to the foreground process. By default that terminates the program, but programs can catch it, for example to save work or ask for confirmation first.
Does Windows have Unix signals?
Not natively. Windows uses its own mechanisms, such as console control events for Ctrl+C, although its C runtime supports a handful of signals like SIGINT for compatibility.
See also
- ProcessOperating Systems, p. 23A process is a running instance of a program, with its own memory space, resources, and at least one thread of execution managed by the operating system.
- InterruptOperating Systems, p. 16An interrupt is a signal to the CPU that an event needs immediate attention, making it pause its current work and run a special handler in the kernel.
- DaemonOperating Systems, p. 7A daemon is a program that runs in the background without a user interface, usually started at boot, to provide a service such as logging, scheduling, or SSH.
- KernelOperating Systems, p. 17A kernel is the core part of an operating system that manages the CPU, memory, and hardware devices and controls how programs get access to those resources.
- ShellOperating Systems, p. 27A shell is a program that reads commands typed by a user or written in a script and asks the operating system to run them, usually through a text interface.
- PodDevOps & Cloud, p. 41A pod is the smallest deployable unit in Kubernetes: one or more containers that share a network address and storage and are scheduled together on one node.
Spotted a mistake or something missing on this page?Suggest an edit