Shell Script - Handling Signals in Shell Script

In this section, let us see how to handle interrupt signal in Shell script.

In a Linux machine, any running application can be interrupted or stopped by interrupting with signals.

For example, assume a Java process is getting executed which has process id "1234".
This Java process can be interrupted by sending SIGKILL(9) signal using the below command, kill -9 1234

If this signal interrupts our application (i.e., Shell script) then it will get terminated abruptly. The initialization and processing that has been done before this signal will be left half way through and your application may not work properly in the next execution. In-order to avoid such condition, it is mandatory that we need to do clean up operation during exit.

To understand better, consider a scenario where during the application start up, you are creating some temporary files which are used for storing some external data and also you need to do some post processing operation during the exit of your application. When your application gets terminated abruptly, all the temporary files which were created during start up will be left as it is in local file system which may not be needed further and also your post processing operation is not performed. In such case, if we start our application again, it may not work properly if there are any dependency with the files created or with the post processing operation.

In order to avoid such conditions, its better to do post processing operation on exit whether it a normal or abrupt exit. But the question is how to do post processing under abrupt exit? Do we get hook when our application gets terminated? Is it possible in Shell script? The answer is "YES".

Shell script has the function named "trap" to handle signals whether its an interrupt or other. By using this command, we can instruct our application to handle those signals like SIGHUP, SIGINT, SIGTERM etc.

By this way, when the User intrudes these signals to our application, the script will detect those signal and invoke our hook ie., method in the Shell script where we can decide whether to exit or to continue.

The below script is simulation to test signal handling. The script will execute continuously in a while loop and when any interrupt signal is received, then it will call the exit_hook method. In the exit_hook method, we are prompting the user with options whether to exit or to continue running the application.

# This function gets executed when the script execution is interrupted by the signals like SIGHUP/SIGINT/SIGTERM
function exit_hook ()
{
        echo "++++++++++++++++ You have selected to Exit the Application ++++++++++++++++"
        echo "Please select one of the below options"
        echo "0 - Continue"
        echo "1 - Exit"
        echo "Enter the option:"
        read input

        if [ ${input} -eq 1 ]
        then
                # Note: Place your Post processing logic here if User wants to exit the application.
                echo "Exiting the application"
                exit 1
        fi
}

# command to trap the signals
trap exit_hook SIGHUP SIGINT SIGTERM

# Initialization done during script start up

# This is a simulation which runs infinitely in order to interrupt the script execution
i=0;
while [ ${i} -eq 0 ]
do
        sleep 1
        echo "Application is running ..."
done

In the below script execution, User has interrupted the program by either pressing Ctrl+C or by the command "kill -9
"
Now the script stops its execution and prompts user with the options, whether to continue or exit. Since User has selected to continue, it doing its processing again. Now, the User has again interrupted the script execution and decided to exit the application. Hence it stopped.

Run TrapSignal.sh

$ sh TrapSignal.sh
Application is running ...
Application is running ...
Application is running ...
Application is running ...
++++++++++++++++ You have selected to Exit the Application ++++++++++++++++
Please select one of the below options
0 - Continue
1 - Exit
Enter the option:
0
Note : You have selected to continue the script execution
Application is running ...
Application is running ...
Application is running ...
Application is running ...
Application is running ...
++++++++++++++++ You have selected to Exit the Application ++++++++++++++++
Please select one of the below options
0 - Continue
1 - Exit
Enter the option:
1
Exiting the application
Technology: 

Search