To understand this we are taking an example script :
#!/bin/bash echo "pid is $$" while (( count < 10 )) do sleep 5 (( count++ )) echo $count done exit 0
In the first line of the script we want to get the PID ( a PID is a unique number that identifies each of the running processes in an operating system) of the script itself which we get by using $$.
Then we have While loop to print numbers till 10, with the delay of 5 secs in every loop by using sleep command, and when the script ends successfully we exit out of the script using exit signal 0. Now execute your script.
- While running the script user may press Ctrl+C in order to come out of the script. So the Ctrl+C signal is provided and the script ended. This Ctrl+C signal is called interrupt signal and in signal terms it is called SIGINT. So basically we have sent SIGINT command to the script to end the execution.
- Now again we will execute the script, this time we get a different PID for the script. We can exit the script by using Ctrl+Z, this Ctrl+Z signal is called suspend signal and in signal terms it is called SIGTSTP.
- Again we will run the script and this time we terminate it by using kill command. Open other terminal and use kill command to terminate the script.
$ kill -9 3414
Along with the kill command we use -9, which is a signal called SIGKILL signal. Then we write the PID name.
In all of the above cases the script was in the middle of doing something and signal was given to interrupt or it may encounter some unexpected behavior.
Trap statements :
There can be situations when we don’t want users to exit script, untimely using keyboard abort sequences. The trap statement catches these sequences and can be programmed to execute a list of commands upon catching those signals.
Now if we will use trap statement it will be able to catch kill command, Ctrl+C or Ctrl+Z and clean it up within the script.
Using the following command we can open the man page for Signal:
$ man 7 signal
When we scroll down we get a table about signals with their value, action and comment. For example we used Ctrl+C command which was SIGINT having value 2. So every signal has some value. We can also see the signal value for Ctrl+Z (SIGTSTP) which is 18,20,24 and for Kill command (SIGKILL) which is 9. You can use all the signals given in the table to terminate process of your script.
Now we will use trap statement in the script we have written above :
Syntax for the trap statement is :
#!/bin/bash trap [commands] [signals]
So in the script we have used trap statement after that we write the command, here we are using echo command and then the signal which we want to be detected. You can either write the name of the signal or just write the value of the signal. Then run the script.
#!/bin/bash trap "echo Exit signal is detected" SIGINT #You can also use the value of the signal: 2 echo "pid is $$" while (( count < 10 )) do sleep 5 (( count++ )) echo $count done exit 0
- After running the script we want to terminate the script using Ctrl+C that is SIGINT, but instead of terminating the process it will print “Exit signal is detected” and then continue the script. Each time you try to terminate the script same thing will happen.
- We will make change in our script in the trap statement we will use SIGKILL signal and then run the script.
#!/bin/bash trap "echo Exit signal is detected" SIGKILL #You can also use the value of the signal: 9 echo "pid is $$" while (( count < 10 )) do sleep 5 (( count++ )) echo $count done exit 0
This time we will terminate the script using Kill command that is SIGKILL, even after using trap statement the process gets terminated.
Because there are some exceptions, trap statement cannot catch signals SIGKILL and SIGSTOP.
Let’s use different commands using trap statements but before writing the script we need to create a new file.
$ touch test.txt
First we declare a variable “file” and give value to the variable that is the path of the file. Then we write the trap statement, use command rm -f, for force deletion and then variable name “file” and then exit command. After that we mention catch signals, 0 that is success signal, 2 that is SIGINT and 15 that is SIGTERM. Then run the script.
#!/bin/bash file=/home/nupur/text.txt trap "rm -f $file; exit" 0 2 15 echo "pid is $$" while (( count < 10 )) do sleep 5 (( count++ )) echo $count done exit 0
Let’s type trap command and see what happens :