Signup/Sign In

What are Linux Signals and How SIGINT, SIGTERM and SIGKILL works

Posted in Programming   LAST UPDATED: MARCH 19, 2023

    Are you a beginner trying to learn the Linux Operating system? Let's learn the basics of the Linux/Unix Operating System in this article.

    There are many Linux signals that are used to make software interrupts in Linux and Unix OS.

    Today, we will learn about these signals, how Linux signals work, and how many Linux signals are there: SIGINT, SIGTERM, and SIGKILL, let's know how these signals work.

    how linux signals work

    What are Signals in Linux?

    When an important action occurs in the system, a signal is sent to the program to specify that the event has occurred. Signals are called interrupts because they interrupt the flow of execution of the program.

    They are like notifications to the system and there are various events like user requests and memory access errors.

    Linux/Unix Operating System has different processes, they are either operating system processes or user application processes. To coordinate the activities of the kernel and these various processes, a mechanism is required. That's why signals are used to notify the Linux system of vital errors.

    In the later versions of Linux, real-time signals are added. Signal interprocess is an efficient and lightweight form of communication and it is compatible with embedded systems.

    What are different signals in Linux?

    There are two types of signals:

    • Maskable
    • Non-Maskable

    Maskable - Maskable signals are signals that a user can ignore or change

    Non-Maskable - Non-Makable is just the opposite of the Maskbale signals, users can change or ignore these signals.

    Linux Kernel has about 30 signals ranging from 1-30. Signal names mostly explain what the signal does. Signal number 9 or SIGKILL notifies the program that the user tries to kill it.

    Here is the main screen of htop. In order to install this handy utility, just type sudo apt install htop on Ubuntu/Mint, or sudo yum install htop on RedHat/Centos/Fedora.

    You can see the number of terminations and other signals which can be sent to a process. You can select a process by pressing the cursor up/down button and then send a signal by using the F9 button.

    SIGINT

    SIGINT works as an interruption request sent by the user to the program. It's a request so how it is handled is depend on the process and the situation.

    When you press Ctrl + C, the SIGINT signal is sent to the program. it is to terminate the process but some programs handle it differently or override this action.

    When using the bash interpreter, if we press Ctrl+C the bash interpreter prints a new and empty prompt line instead of quitting. Similarly, when we use gdb as a debugger, we can send a SIGINT signal by pressing the Ctrl+C to stop the execution of the program.

    Let’s execute the demo_sigint.sh using the trap command to handle SIGINT and print the current date:

    #!/bin/bash
    
    trap date SIGINT
    
    read input
    echo User input: $input
    echo Exiting now

    Now, that we've run the script, let's press Ctrl+C

    You can see that the script doesn't exist. If you want to terminate the script, you can't use SIGINT, you need to use IGTERM, SIGQUIT, or SIGKILL instead.

    SIGTERM

    SIGTERM signal is the default signal to kill a program by using the kill command. When using SIGTERM, you are requesting to terminate the program.

    SIGTERM and SIGQUIT are kind of similar, both specify to terminate the process.

    You can handle SIGTERM to ask the user before exiting. Let's create a script to terminate if you run the kill <PID> twice:

    #!/bin/bash
    
    SIGTERM_REQUESTED=0
    demo_sigterm() {
        if [ $SIGTERM_REQUESTED -eq 0 ]; then
            echo "Send SIGTERM again to terminate"
            SIGTERM_REQUESTED=1
        else
            echo "SIGTERM received, exiting now"
            exit 0
        fi
    }
    
    trap demo_sigterm SIGTERM
    
    TIMEOUT=$(date +%s)
    TIMEOUT=$(($TIMEOUT + 60))
    
    echo "This script will exit in 60 seconds"
    while [ $(date +%s) -lt $TIMEOUT ]; do
        sleep 1;
    done
    echo Timeout reached, exiting now

    Let's run this script in the background and use the kill <PID> command twice:

    $ ./demo_sigterm.sh &
    [1] 6092
    $ kill 6092
    Send SIGTERM again to terminate
    $ kill 6092
    SIGTERM received, exiting now
    [1]+  Done                    ./demo_sigterm.sh

    Alright, now look that the script is exited when the second kill command is called.

    SIGKILL

    SIGKILL is a signal that can't be ignored and if a process receives a SIGKILL signal then the process is terminated.

    It's a Non-Maskable signal which can't be ignored and can't be changed.

    You can first send SIGTERM to give the process some time to terminate. You can send the SIGTERM multiple times to terminate the process.

    If the process doesn't terminate on its own then you can end the SIGKILL signal that will terminate the process instantly.

    Let's use the previous script to handle the SIGKILL:

    #!/bin/bash
    
    SIGKILL_REQUESTED=0
    demo_sigkill() {
        if [ $SIGKILL_REQUESTED -eq 0 ]; then
            echo "Send SIGKILL again to terminate"
            SIGKILL_REQUESTED=1
        else
            echo "Exiting now"
            exit 0
        fi
    }
    
    trap demo_sigkill SIGKILL
    
    read input
    echo User input: $input

    Let's run the script and use the kill -SIGKILL <PID>:

    $ ./demo_sigkill.sh
    Killed
    $

    The process terminated without asking to re-send the signal.

    Sigterm VS Sigkill

    Sigterm is a Linux signal that kills a program using a command called kill while Sigkill is a Linux signal that a process can't ignore because it can terminate the process when received. let's see the difference between Sigterm VS Sigkill

    Signal Name Full Name Action Catchable Graceful Termination Child Process
    SIGTERM Signal Termination Requests a process to terminate gracefully Yes Yes Does not kill child processes
    SIGKILL Signal Kill Requests a process to terminate immediately and forcefully No No Kills child processes as well

    Sigterm VS SigInt

    SIGTERM and SIGINT are two common signals in Linux, both used to request a process to terminate. However, they differ in the way they request termination:

    Signal Name Full Name Action Catchable Use Cases
    SIGTERM Signal Termination Requests a process to terminate gracefully Yes Normal process termination
    SIGINT Interrupt Signal Requests a process to terminate by interrupting it Yes User-initiated process termination

    Conclusion

    Now, you know What are Linux Signals and How SIGINT, SIGTERM, and SIGKILL signal is works. SIGINT is sent when you press Ctrl+C. SIGTERM and SIGKILL are used to terminate the process.

    FAQ

    Following are some frequently asked questions.

    1. How signals are generated in Linux?

    Signals are generated by a process via the kernel.

    2. How many signals does Linux have?

    Linux has 31 signals.

    About the author:
    Proficient in Java, Python, and web development; has a knack for writing clearly about complex topics. Dedicated to giving readers the tools they need to learn more about computer science and technology.
    Tags:linux-signalsunixlinux
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS