Understanding signals and traps for Linux shell script

April 30, 2021

Comments

Adsense

Adsense

Adsense

Adsense

Two types of interrupts exist in 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 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 Ctrl + C. 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 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 to Ctrl + 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:

[email protected]:/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 presses Ctrl + C.

Termination

3

SIGQUIT

The user presses Ctrl + \.

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, the kill command sends signal number 15 to the process. Using the kill command, we can send the desired signal to any specific process.

We can suspend a process using the Ctrl + Z signal as follows:

$ kill -19 pid

Ctrl + Z or SIGTSTP will suspend the process.

We can restart the suspended process by sending the SIGCONT signal.

$ kill -18 pid

The signal number of SIGCONT is 18.

Satish Kumar

Satish Kumar

I am Satish Kumar, Founder of LinuxConcept. Linux and F.O.S.S enthusiast, love to work on open source platform and technologies.

Adsense

Adsense

Adsense

Adsense

Adsense

Adsense

Adsense

Adsense

Adsense

Related Posts

Backup of files in Linux using command line

In IT or our day-to-day computer industry activities, taking backup is one of the most important activities. Previously, offices were required to keep important paper in a safe place; but if a fire breaks out, then everything is finished. In the digital world, taking...

Using awk in Linux

awk is a program that has its own programming language for performing data-processing and generating reports. The GNU version of awk is gawk. awk processes data, which can be received from a standard input, input file, or as the output of any other command or process....

0 Comments

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

8 + eighteen =

News & Updates

Join Our Newsletter

Adsense

Adsense

Adsense

Adsense