Skip to content

Linux Foundations / Manage a Linux host ยท Step 27 of 50

Lesson: SignalsSwitch
โ† Linux Foundations

System administration

Signals

Learn what signals are, the common ones, and how programs react to them.

A signal is a short notification sent to a process. It carries almost no data: mainly just which signal it is. The kernel sends signals when something happens, and users and programs can send them too.

The common ones

| Name | Number | Typical cause or use | | --- | --- | --- | | SIGHUP | 1 | terminal closed; many daemons reload settings | | SIGINT | 2 | Ctrl+C | | SIGKILL | 9 | forced end | | SIGSEGV | 11 | invalid memory access | | SIGTERM | 15 | polite request to end | | SIGCONT | 18 | resume a stopped process | | SIGSTOP | 19 | pause a process | | SIGTSTP | 20 | Ctrl+Z |

The numbers 18 to 20 are those used on x86 and ARM. Some architectures differ, so prefer names. List them all with:

bash
$ kill -l
 1) SIGHUP   2) SIGINT   3) SIGQUIT   4) SIGILL   5) SIGTRAP
...

What a process can do

For most signals, a process has three choices:

  1. Accept the default action, which might be to end, stop, continue or ignore.
  2. Ignore the signal.
  3. Catch it with a handler function that runs when it arrives.

SIGKILL and SIGSTOP are the exceptions. They cannot be caught, blocked or ignored, so the system always has a way to end or pause a process.

A process may also block a signal for a while. The signal then stays pending and is delivered once unblocked.

Catching a signal in a script

The Bash builtin trap sets a handler:

bash
#!/bin/bash
trap 'echo "cleaning up"; exit 0' TERM INT
while true; do
    sleep 1
done

Save it as loop.sh, run bash loop.sh, then press Ctrl+C:

bash
$ bash loop.sh
^Ccleaning up

Real programs use this to save data or remove temporary files before they leave.

Reload by convention

Many services treat SIGHUP as "read your configuration again". It is only a convention. A program that does not handle SIGHUP will be ended by it, so check the documentation first. Where a service manager is available, a command such as systemctl reload nginx is the safer way.

Try it

Run kill -l, then kill -l 15 to turn a number into a name. Run the script above and send it a signal from a second terminal with kill -TERM PID. The processes and cron lab has a shell ready, and man 7 signal or the online signal manual lists every default action.

Check your understanding

Signals

All 4 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.