What a signal is
A signal is a small integer that the kernel delivers to a process at any time. The process can be running, sleeping, or stopped when the signal arrives. The kernel does not send the whole message, only the number. Each signal has a default action, such as terminate, ignore, stop, or continue. A process can change some of those actions with a handler, and it can block some signals so they wait in a queue until it unblocks them.
Signals are the mechanism behind several keys you already know. Ctrl-C sends SIGINT, Ctrl-Z sends SIGTSTP, and Ctrl-\ sends SIGQUIT. The terminal line discipline generates those signals for the foreground process group, as described in the terminal lesson earlier in this course.
Listing the signal names
The kill builtin in bash can list every signal name with its number. The -l option prints the list, and when you give it a number it prints the name for that number. Some signals have the same number on every Linux system, while others vary by architecture, so use the list on your system when a number matters.
kill -l
kill -l 15
kill -l 9The first command prints the whole table on one set of lines. The second prints TERM, because signal 15 is SIGTERM. The third prints KILL, because signal 9 is SIGKILL. The most common signals are HUP (1), INT (2), QUIT (3), KILL (9), USR1 (10), USR2 (12), TERM (15), CONT (18), STOP (19), and TSTP (20) on Linux.
Sending a signal
The kill command takes a signal, then one or more PIDs. You can name the signal by number, as in kill -9 PID, or by name with or without the SIG prefix, as in kill -TERM PID. Without a signal option, kill sends SIGTERM. A program that receives SIGTERM is expected to clean up and exit, so it is the polite first choice.
The following block sends a user signal to the shell itself, after installing a trap that prints a message when the signal arrives.
trap "echo got USR1" USR1
kill -USR1 $$
echo back in the shellThe trap runs as soon as the shell handles the signal. The message appears before the final echo, because the signal is delivered to the shell at the next safe point. The shell continues afterward, which shows that a handled signal does not end the process.
SIGKILL and SIGTERM
SIGKILL cannot be caught, blocked, or ignored. The kernel terminates the process directly, and the process gets no chance to flush files or remove temporary data. SIGTERM is different: it can be handled, so a program can save state before it exits. For that reason you should send SIGTERM first and use SIGKILL only when a process does not respond.
SIGSTOP is the stop signal that also cannot be caught, and SIGCONT resumes a stopped process. Ctrl-Z is the interactive version of SIGTSTP, which a program can catch, while SIGSTOP always stops the process.
Do not send SIGKILL to a process unless SIGTERM has failed. The process cannot clean up, so it may leave lock files or half-written data behind.
The exit status after a signal
When a process is killed by a signal, the shell does not see a normal exit code. It reports 128 plus the signal number in $?. A program that ends with SIGTERM therefore shows 143, and one that ends with SIGINT shows 130. The kill -l command understands this convention, so you can convert the status back into a name.
kill -l 143
kill -l 130These commands print TERM and INT. The arithmetic is 143 minus 128, which is 15, and 130 minus 128, which is 2. This is the same convention that the first lesson of the course used for Ctrl-C.
Signals also explain a common surprise with scripts. A script that traps SIGTERM to clean up will not exit when it receives the signal until its handler returns, and a long command inside the handler delays the exit. This is why a handler should do the minimum, set a flag, and let the main loop check the flag. The kernel does not wait for the handler, so the signal is delivered at once, but the work still has to finish before the process can end.
Finally, remember that a signal goes to a process, not to a command line. If a pipeline has three stages, a signal sent to one PID affects only that stage, and the others keep running until their input ends. To stop the whole pipeline, send the signal to every stage or to the process group. The job control lesson that follows shows how the shell groups the stages of a pipeline together so that a single Ctrl-C reaches all of them.
Hands-on: send and observe
Start a background sleep, record its PID in a file, and send it SIGTERM. The sleep does not catch the signal, so it stops at once. The lesson is in the status: the job ends by signal, not by normal completion.
sleep 300 & echo $! > job.pid
kill -TERM "$(cat job.pid)"
wait "$(cat job.pid)"; echo "status $?"The final status is 143, because the sleep ended with SIGTERM. Run kill -l 143 to see the name. The PID you record in the file changes each time, which is why the lab check reads the file rather than expecting a fixed number.