Home » BASH » Write a bash script for generating and trapping signals for cleanup

Write a bash script for generating and trapping signals for cleanup

Update on:
Oct 5, 2021

You have probably pressed Ctrl + C or Ctrl + Z without knowing what was occurring—it’s just like pressing Ctrl + Alt + Delete in another OS, right? Well, in one regard, yes—it is a signal, but the action itself is very different in Linux. A signal at the hardware level is similar to a flag or some sort of immediate notification that says hey – something happened here. If the appropriate listener is set up, that signal can execute some sort of functionality.

On the other hand, software signaling is far more flexible and we can use signals as simple notification mechanisms that are far more flexible than their hardware siblings. In Linux, Ctrl + C equates to SIGINT (program interrupt), which typically exits a program. It can be stopped, and other functionality such as cleanup can be executed. Ctrl + Z or SIGTSTP (keyboard stop) typically tells a program to be suspended and pushed to the background (more about jobs in a later section), but it can also be blocked—just like SIGINT.

Prerequisites

Besides using the keyboard within a program, we can also send signals to programs using the kill command. The kill command can kill programs, but these signals can also be used for reloading configurations or sending user-defined signals. The most common signals you may use are SIGHUP (1), SIGINT (2), SIGKILL(9), SIGTERM(15), SIGSTOP(17,18,23), SIGSEGV(12), and SIGUSR1(10)/SIGUSR2(12). The latter two can be defined within your program or leveraged by other developers. 

The kill command can be used easily as follows:

$ kill -s SIGUSR1 <processID> 
$ kill -9 <processID> 
$ kill -9 `pidof myprogram.sh`

Note:

The kill command can refer to the signal number itself or by its name. It does require a process ID number to target. This can easily be found either by searching using ps | grep X or by using the preceding final using pidof.

Write Script:

Let’s start our activity as follows:

Open a terminal and begin a new script called mytrap.sh with the following contents:

mytrap.sh

#!/bin/bash 
function setup() { 
	trap "cleanup" SIGINT SIGTERM 
	echo "PID of script is $$" 
} 
function cleanup() { 
	echo "cleaning up" 
	exit 1 
} 
setup 

# Loop forever with a noop (:) 
while : 
do 
	sleep 1 
done

Execute the script with $ bash mtrap.sh.

Press Enter several times and watch the behavior of the program.

Press Ctrl + C; notice anything different?

How this script works:

Let’s understand our script in detail:

The mytrap.sh script leverages functions and the trap call. Inside of the setup function, we set the function to be called by the trap command. Therefore, when Ctrl + C is called, the cleanup function is executed.

Running the script will cause the script to run forever after printing out the PID of the script.

Pressing regular keys such as Enter will not have an effect on the program.

Pressing Ctrl+C will echo cleanup on the console and the script will exit using the exit command.

Related Posts

Creating a config file and using it in tandem with your scripts

In this article, we are going to create a config file and use it in our shell script.PrerequisitesBesides having a terminal open, you need basic knowledge of creating scripts and config files.Write scriptNow, we are going to create a script and config file. The...

Calculating and reducing the runtime of a script

In this article, we are going to learn how to calculate and reduce the script’s runtime. A simple time command will help in calculating the execution time.PrerequisitesBesides having a terminal open, make sure you have the necessary scripts present in your...

Using Bash to monitor battery life and optimize it

In this article, we will learn about the TLP Linux tool. TLP is a command-line tool; it is used for power management and will optimize the battery life.PrerequisitesBesides having a Terminal open, you need to ensure that you have TLP installed on your system.How to do...

Creating a simple NAT and DMZ firewall using bash script

In this article, we will create a simple NAT firewall with DMZ using iptables.PrerequisitesBesides having a Terminal open, you need to ensure that iptables is installed in your machine.Write scriptWe will write a script to set up a DMZ using iptables. Create...

Follow Us

Our Communities

More on BASH

The Ultimate Managed Hosting Platform
Load WordPress Sites in as fast as 37ms!

0 Comments

0 Comments

Submit a Comment

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

15 + fourteen =

Shares