Ignoring signals in Linux for bash script

May 2, 2021

Comments

Adsense

Adsense

Adsense

Adsense

If we want the shell to ignore certain signals, then we can call the trap command followed by a pair of empty quotes as a command. Those signals will be ignored by the shell process shown by either of the following commands:

$ trap " " 2 3 20$ trap " " INT QUIT TSTP

The signals 2 (SIGINT)3 (SIGQUIT), and 20 (SIGTSTP) will be ignored by the shell process.

Resetting signals

If we want to reset the signal behavior to the original default action, then we need to call the trap command followed by the signal name or number as shown in the following examples, respectively:

$ trap TSTP$ trap 20

This will reset the default action of signal 20 (SIGTSTP). The default action is to suspend the process (Ctrl + Z).

Listing traps

Let’s reassign our function to signals with the trap command:

$ trap 'echo "You pressed Control key" '  0 1 2 15

If we do not pass any arguments after the trap command, then it lists all reassigned signals along with their functions.

We can list all the assigned signal lists with the following command:

$ trap

Output:

Output:

trap -- 'echo "You pressed Control key" ' EXIT
trap -- 'echo "You pressed Control key" ' SIGHUP
trap -- 'echo "You pressed Control key" ' SIGINT
trap -- 'echo "You pressed Control key" ' SIGTERM
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 *

14 − twelve =

News & Updates

Join Our Newsletter

Adsense

Adsense

Adsense

Adsense