Magazine

Understanding Signals and Traps for Linux Shell Script

Posted on the 30 April 2021 by Satish Kumar @satish_kumar86

Two types of interruptsexistin the Linux operating system: hardware interrupts and software interrupts. Software interrupts are called signals or traps. Software interrupts are used for inter-process synchronizations.

Signals are used to notify us about a certain event occurrence or to initiate a certain activity.

We use software signals many times. For example, if any command does not respond after being typed, then you might have enteredCtrl+C. This sends aSIGINTsignal 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 usingCtrl+C. In such cases, we can use thetrapcommand 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 sometimes due to power failure, system hang up, illegal instruction execution, or invalid memory access.

The action, performed by a few signals, terminates the process. We can configure the shell to make the following responses:

  • Catch the signal and execute user-defined programs
  • Ignore the signal
  • Suspend the process (similar toCtrl+Z)
  • Continue the process, which was suspended earlier

Enter either of the following commands to get the full list of all signals:

$ kill -l
$ trap -l

Output:

root@linux:/home/satish# kill -l
 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL       5) SIGTRAP
 6) SIGABRT      7) SIGBUS       8) SIGFPE       9) SIGKILL     10) SIGUSR1
11) SIGSEGV     12) SIGUSR2     13) SIGPIPE     14) SIGALRM     15) SIGTERM
16) SIGSTKFLT   17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU     25) SIGXFSZ
26) SIGVTALRM   27) SIGPROF     28) SIGWINCH    29) SIGIO       30) SIGPWR
31) SIGSYS      34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7
58) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX

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

EXIT

The shell exits.

Termination

1

SIGHUP

The terminal has been disconnected.

Termination

2

SIGINT

The user pressesCtrl+C.

Termination

3

SIGQUIT

The user pressesCtrl+\.

Termination

4

SIGILL

This gives an illegal hardware instruction.

Program error

5

SIGTRAP

This is produced by the debugger.

Program error

8

SIGFPE

This gives an arithmetical error, such as division by zero.

Program error

9

SIGKILL

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, thekillcommand sends signal number15to the process. Using thekillcommand, we can send the desired signal to any specific process.

We can suspend a process using theCtrl+Zsignal as follows:

$ kill -19 pid

Ctrl+ZorSIGTSTPwill suspend the process.

We can restart the suspended process by sending theSIGCONTsignal.

$ kill -18 pid

The signal number of SIGCONT is 18


Back to Featured Articles on Logo Paperblog