Open In App

Signals and traps in Unix

In this article, we will discuss the overview of Signals and traps in Unix and understand it with the help of an example. And finally will conclude different scenarios for signals and traps in Unix. Let’s discuss it one by one.

Overview :



Example –
If you’ve written any amount of bash code, you’ve almost certainly encountered the trap command. Trap enables you to capture signals and run code when they occur. Signals are asynchronous notifications delivered to your script when specific events occur. The majority of these alerts are for situations that you hope never occur, such as an incorrect memory access or a poor system call. However, there are one or two occurrences that you may wish to address. There are additional “user” events that are never created by the system and may be used to signal your script. Bash also has a pseudo-signal named “EXIT” that is performed when your script terminates.

Implementation in python :






#GeeksforGeeks
trap itsatrap 1 2 3 7
  
#cleanup method
cleanup()
{
  echo "We got the signal to clean, beginning"
  rm -rf /tmp/temp_*.$$
  echo "Done! ... exiting now!."
  exit 1
}
# for loop
for i in *
do
  sed s/FOO/BAR/g $i > /tmp/temp_${i}.$$ && mv /tmp/temp_${i}.$$ $i
done

Caution – 
DO NOT RUN THE ABOVE SCRIPT, AS IT’s A TRAP!

Typical Unix/Linux activities :

Different Scenarios for Signals and traps in Unix :

  1. If there are several commands set to trap, they must be surrounded by citations. You’ll also note that the command line shell scans when the trap command is executed and again when one of the mentioned signals is received.
  2. If you disregard a signal, all the additional shells will do the same. However, if you specify an action to be done when the signal is received, all sub-shells will still conduct the action when the signal is received.
  3. There may be times when you don’t want users of your scripts to quit prematurely by utilizing keyboard abort sequences, such as when input is required or cleaning is required. The trap statement intercepts these sequences and may be designed to execute a set of commands when they are intercepted.
  4. When Bash receives a signal for which a trap has been set while waiting for a command to finish, the trap is not performed until the command is finished.
  5. When Bash is waiting for an asynchronous command using the wait built-in, the arrival of a signal for which a trap has been set causes the wait built-in to return immediately with an exit status larger than 128, and the trap to be performed.
  6. When one or more specified signals are received, this command defines the action to be done. When the shell gets a certain signal, it performs the action indicated.
  7. Specifies the signal to be trapped. You have the option of specifying a signal number or a signal name. When you specify a signal name, you must omit the leading SIG (for example, specify INT for SIGINT). The requirements for each signal may be found in the documentation for the operating system being utilized. 
  8. It should be noted that SIGTERM cannot be specified.

Article Tags :