In the last chapter, you learned about using functions in Shell scripts and defining, displaying, and removing functions from the shell. You also learned about passing arguments to functions, sharing data between functions, declaring local variables in functions, returning results from functions, and running functions in the background. In the end, you learned about using source and . commands. You used these commands for using a library of functions.
In this chapter, you will learn the following topics:
dialog utilityTwo types of interrupts exist in the Linux operating system: the hardware interrupt and the software interrupt. Software interrupts are called signals or traps. Software interrupts are used for interprocess synchronizations.
Signals are used to notify about a certain event occurrence or to initiate a certain activity.
We use software signals many times, for example, if any command is not responding after it is typed, then you might have entered Ctrl + C. This sends a SIGINT signal to the process, and the process is terminated. In certain situations, we may want the program to perform a certain activity instead of terminating it using the Ctrl + C command. In such cases, we can use the trap command to ignore a signal or to associate our desired function with that signal.
In operating systems, software interrupts or signals are generated when the process attempts to divide a number by zero or due to power failure, system hang up, illegal instruction execution, or invalid memory access.
The action performed by a few signals is to terminate the process. We can configure the shell to do the following responses:
Enter either of following commands to get the full list of all signals:
$ kill -l $ trap –l
Output:

If we want to know which keys are used for particular signals, then we enter the following command:
$ stty -a
The following is a list of a few of the standard signals that a process can use:
|
Number |
Name |
Description |
Action |
|---|---|---|---|
|
0 |
|
The shell exits. |
Termination |
|
1 |
|
The terminal has been disconnected. |
Termination |
|
2 |
|
The user presses Ctrl + C |
Termination |
|
3 |
|
The user presses Ctrl + \ |
Termination |
|
4 |
|
This gives an illegal hardware instruction. |
Program error |
|
5 |
|
This is produced by debugger. |
Program error |
|
8 |
|
This gives an arithmetic error, such as division by zero. |
Program error |
|
9 |
|
This cannot be caught or ignored. |
Termination |
We can send either of the kill signals to a process with PID # 1234 as follows:
kill -9 1234 kill -KILL 1234 kill -SIGKILL 1234
As we can see, we can use a signal number or a signal name along with the process ID. By default, the kill command sends signal number 15 to process. Using the kill command, we can send the desired signal to any specific process.
We can stop a process using the Ctrl + Z signal as follows:
$ kill -19 pid
Ctrl + Z or SIGTSTP will stop the process.
We can run the stopped process by sending the SIGCONT signal.
$ kill -18 pid
The signal number of SIGCONT is 18.