UNIX / Linux Signals and Traps - Unix/Linux

What are UNIX / Linux Signals and Traps?

In this section, we will discuss in fact about Signals and Traps in UNIX.

Signals are software interrupt sent to a program to point out that an important event has occurred. The actions can vary from user requirements to illegal memory access errors. Some signals, such as the interrupt signal, designate that a user has ask the program to do something that is not in the usual flow of control.

The following table lists out general signals you might encounter and want to use in your programs −

Signal Name Signal Number Description
SIGHUP 1 Hang up detected on controlling terminal or death of controlling process
SIGINT 2 Issued if the user sends an interrupt signal (Ctrl + C)
SIGQUIT 3 Issued if the user sends a quit signal (Ctrl + D)
SIGFPE 8 Issued if an illegal mathematical operation is attempted
SIGKILL 9 If a process gets this signal it must quit immediately and will not perform any clean-up operations
SIGALRM 14 Alarm clock signal (used for timers)
SIGTERM 15 Software termination signal (sent by kill by default)

List of Signals

There is an easy way to list down all the signals support by your system. Just issue the kill -l command and it would present all the support signals –

The actual list of signals varies between Solaris, HP-UX, and Linux.

Default Actions

Every signal has a default action connected with it. The default action for a signal is the action that a script or program performs when it receive a signal.

Some of the achievable default actions are −

  • Terminate the process.
  • Ignore the signal.
  • Dump core. This creates a file called core containing the memory image of the process when it received the signal.
  • Stop the process.
  • Continue a stopped process.

Sending Signals

There are several methods of delivering signals to a program or script. One of the mainly common is for a user to kind CONTROL-C or the INTERRUPT key while a script is execute.

When you press the Ctrl+C key, a SIGINT is sent to the script and as per defined default action script terminates.

The other general scheme for delivering signals is to use the kill command, the syntax of which is as follows –

Here signal is both the number and name of the signal to deliver and pad is the procedure ID that the signal should be sent to. For instance –

The above command sends the HUP or hang-up signal to the program that is running with process ID 1001. To send a kill signal to the same process, use the following command –

This kills the process running with process ID 1001.

Trapping Signals

When you press the Ctrl+C or Break key at your mortal throughout execution of a shell program, normally that program is instantly terminated, and your command prompt returns. This may not always be desirable. For illustration, you may end up leaving a bunch of impermanent files that won't get cleaned up.

Trapping these signals is quite simple, and the trap command has the following syntax –

Here command can be any applicable UNIX command, or even a user-defined function, and signal can be a list of any number of signals you want to trap.

There are two regular uses for trap in shell scripts −

  • Clean up temporary files
  • Ignore signals

Cleaning Up Temporary Files

As an instance of the trap command, the following show how you can remove some files and then way out if someone tries to abort the program from the workstation –

From the point in the shell program that this trap is execute, the two files work1$$ and data out$$ will be automatically isolated if signal number 2 is arriving by the program.

Hence, if the user interrupts the implementation of the program after this trap is execute; you can be assured that these two files will be cleaned up. The exit command that follows the ram is necessary because without it, the execution would continue in the program at the point that it left off when the signal was external.

Signal number 1 is generating for hang-up. Either someone intentionally hangs up the line or the line gets by chance disconnected.

You can change the preceding trap to also eliminate the two individual files in this container by adding signal number 1 to the list of signals –

Now these files will be removed if the line gets hung up or if the Ctrl+C key get press.

The commands particular to trap must be enclosed in quotes, if they contain more than one command. Also note that the shell scans the command line at the time that the trap command gets executed and also when one of the listed signals is external.

Thus, in the following instance, the value of WORKDIR and $$ will be substituted at the time that the trap command is execute. If you wanted this substitution to happen at the time that either signal 1 or 2 was external, you can put the commands inside single quotes –

Ignoring Signals

If the command listed for trap is null, the specified signal will be mistreated when received. For instance, the command –

This specifies that the suspend signal is to be ignored. You capacity want to ignore definite signals when performing an operation that you don't want to be interrupted. You can specify many signals to be ignored as follows –

Note that the first dispute must be particular for a signal to be mistreated and is not different to writing the following, which has a divide meaning of its own –

If you discount a signal, all sub shells also ignore that signal. Still, if you denote an action to be taken on the reception of a signal, all sub shells will still take the default action on acceptance of that signal.

Resetting Traps

After you've transformed the default act to be taken on reception of a signal, you can change it back again with the trap if you basically exclude the first argument; so –

This reset the achievement to be unavailable on the acceptance of signals 1 or 2 back to the default.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Unix/Linux Topics