Linux Signals: SIGTERM, SIGKILL, SIGHUP and How to Use kill and trap
The "conversation" between the operating system and running programs is through Signals in Linux, which are software interrupts, used to put a program connected to a terminal, on hold, in the background or foreground, and even stop its execution.
Each process in Linux stays listening for signals. These signals are used by the Kernel, by other processes or by users to notify a specific process about a particular event. The UNIX guru W. Richard Stevens describes signals as software interrupts.
When a signal is sent to a process it takes a specific action depending on the value that this signal carries.
Each signal has a unique name and a numeric value.
Let's look at some values and their actions:
TABLE – Most important signals
| Signal | Numeric Value | Action |
| SIGHUP | 1 | Hang-Up or disconnect. This signal is used automatically when you disconnect from a session or close a terminal. It is also used by server processes to invoke the rereading of the configuration file. |
| SIGINT | 2 | This is the interrupt signal. It is triggered for example when you press the Ctrl C keys. |
| SIGKILL | 9 | Terminate the process unconditionally quickly and drastically. This type of signal can leave open files and corrupted databases. Should be used if the process stops responding or in an emergency. |
| SIGTERM | 15 | Terminate the process gracefully, allowing it to close files and execute its shutdown routines. |
| SIGCONT | 18 | This is a signal for a process that was stopped with the SIGSTOP signal so that it continues to execute. |
| SIGSTOP | 19 | This is a signal that can be sent to a process to stop it from functioning temporarily. It still exists, but no longer receives CPU to execute, until it receives the SIGCONT signal. |
| SIGTSTP | 20 | Terminate execution to continue later. This signal is sent automatically when we use the ctrl-z keys. It is used to put the process in the background. |
Linux has over 30 defined signals. Most are used by the Kernel and some by users in specific situations.
Understanding signals is important for knowing how the system interacts with running processes. The kill and killall commands can be used to send specific signals to a running process.
Most administrators when stopping a process use SIGKILL. However, this signal is very "strong", as it makes an abrupt stop in the running process, and can leave open files, malformed indexes, and corrupted data. It is better to use the SIGTERM signal, which allows the program to terminate gracefully, closing files, indexes and databases.
Linux allows users to send signals to running programs through the kill command.
Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.
Liked it? Share
That was just one piece. The course teaches the whole path — from your first command to exam day.