Signals are asynchronous messages
A signal is a small numbered message the kernel delivers to a process. It can arrive at any moment, interrupting whatever the process was doing. A process can handle it with a function, ignore it, or accept the default action, which for most signals ends the process. The number and the name refer to the same thing: SIGINT is 2, SIGTERM is 15, and SIGKILL is 9. The exit status of a process that died from a signal is 128 plus the number.
Who receives the Ctrl-C
The terminal does not send a signal to one process. It sends SIGINT to the foreground process group of the terminal. A process group is a set of processes started together, such as the stages of a pipeline. So when you run a pipeline in the foreground and press Ctrl-C, every stage receives the signal at about the same time. The kernel does this as the line discipline on the terminal reads the control character.
sleep 100 | cat
# press Ctrl-C here, both stages receive SIGINTPressing Ctrl-C ends both sleep and cat and returns the prompt. Neither program had to know about the other. The shell waits for the whole pipeline, then sets $? from the last stage, which for a pipeline killed by SIGINT is 130.
How a script handles the signal
A script runs in the foreground process group like any other program. When SIGINT arrives, bash itself receives it. If bash is waiting for a foreground child, it waits for the child to finish dying, then reacts. If the script has a trap on INT, the trap runs after the child is gone, and the script may continue or exit as the trap says. Without a trap, the default action ends bash, and the script stops.
bash -c 'trap "echo interrupted; exit 130" INT; sleep 100'
# press Ctrl-C; the trap prints interrupted and the script exits 130The trap runs when the sleep ends, which happens because the signal reached the sleep too. The status 130 is the one a well-behaved script chooses for an interruption, because it matches the value a shell reports for death by SIGINT.
Signals sent to one process
The kill command sends a signal to a process ID, and by default it sends SIGTERM. A process that receives SIGTERM can clean up, which is why SIGTERM is the polite way to ask a program to stop. SIGKILL cannot be trapped or ignored, so the kernel removes the process without asking it. Use SIGKILL only when a process does not respond to SIGTERM.
sleep 100 &
pid=$!
kill -TERM "$pid"
wait "$pid"; echo "status=$?"The status is 143, which is 128 plus 15. The $! variable holds the process ID of the last background job, and wait reports how it ended.
Background jobs and SIGINT
A background job started by a non-interactive script has SIGINT and SIGQUIT ignored by default, as POSIX requires when job control is off. That is why Ctrl-C does not stop a background job started in a script, while the same command started in the foreground stops immediately. The behavior is deliberate, so that a background job can finish its own cleanup.
Race conditions around signals
A signal can arrive between two lines of your script, so state can be half written when it does. Cleanup traps should be idempotent, meaning running them twice is harmless, and writes that must be atomic should go to a temporary file that is then renamed into place. Renaming within one file system is atomic, so a reader sees either the old file or the new one.
Hands-on: trap a signal you send yourself
Run the block below. It sends TERM to a child bash that traps the signal. You can run it without a terminal, which makes it a repeatable test of the mechanism.
bash -c 'bash -c "trap \"echo got term; exit 0\" TERM; sleep 30 & wait" & pid=$!; sleep 1; kill -TERM "$pid"; wait "$pid"; echo "status=$?"'The child prints got term and exits with status 0. The outer script then prints the status of the child. The pause of one second gives the child time to start and set its trap before the signal arrives.