Table of contents
- Introduction
- Syntax on how to use trap statement in bash scripts
- Practical examples on using trap statement in bash scripts
- Conclusion
Introduction
Bash scripts have a special feature called trap to execute a code block based on the signals received. Processes in linux machines can receive variety of signals like SIGINT , SIGTERM , SIGQUIT, SIGSEGV , etc
To view different possible Signals sent to a process with their code number , we can use this command
[root@discoveringsystems bash-trap]# kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL
5) SIGTRAP 6) SIGABRT 7) SIGEMT 8) SIGFPE
9) SIGKILL 10) SIGBUS 11) SIGSEGV 12) SIGSYS
13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGURG
17) SIGSTOP 18) SIGTSTP 19) SIGCONT 20) SIGCHLD
21) SIGTTIN 22) SIGTTOU 23) SIGIO 24) SIGXCPU
25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH
29) SIGINFO 30) SIGUSR1 31) SIGUSR2
For example SIGINT is sent to the process when the user hits ctrl +c , likewise the signals are sent to the process to trigger quit or to handle an error.
Syntax on how to use trap statement in bash scripts
Following syntax can be used to call a trap inside the bash scripts. We have created a function with a code block to execute as per the needs . Then we call the function using the trap when a signal is received by the process
function func()
{
#Command statements
}
trap func <SIGNAL to react>
Practical examples on using trap statement in bash scripts
How to use trap statement to execute a code block on receiving signal SIGINT (ctrl + c )
In this example , we are going to use a trap statement in bash script to execute a code block/ function on receiving the signal SIGINT ( ctrl + c ) for terminating the script .
We are running the sleep statement to make the code run for 100 seconds and then we are hitting the “ ctrl + c “ to trigger the signal SIGINT and this is trapped by the trap statement and the code block/ function “code-exit” is executed.
#script
[root@discoveringsystems bash-trap]# cat bash_script
#!/bin/bash
function code-exit()
{
echo " code has been exited by hitting ctrl + c by the user "
}
trap code-exit SIGINT
sleep 100
echo "sleep time is over now"
#script execution and then hit ctrl +c
[root@discoveringsystems bash-trap]# ./bash_script
^C code has been exited by hitting ctrl + c by the user
sleep time is over now
How to use trap statement to execute a code block on receiving signal EXIT
In this example , we are going to use a trap statement in bash script to execute a code block/ function after the code ends properly with the EXIT signal
We are running the sleep statement to make the code run for 10 seconds by calling it under the function code and when the script ends with proper EXIT , this is trapped by the trap statement and the code block/ function “code-exit” is executed.
#script
[root@discoveringsystems bash-trap]# cat bash_script_2
#!/bin/bash
function code-exit()
{
echo " script ended and trap is executed by the signal EXIT "
}
trap code-exit EXIT
function code()
{
sleep 10
echo "sleep time is over now"
}
code
#Script execution
[root@discoveringsystems bash-trap]# ./bash_script_2
sleep time is over now
script ended and trap is executed by the signal EXIT
Conclusion
Similarly we can use the trap statement to trap various signals a process can receive and based on that we can execute the code block as per needs to get the job done