zsh

ZSH Scripting Lesson: Handling Signals and Traps for Script Reliability

In this guide, we will learn about handling signals and traps in ZSH scripts. Signals and traps are incredible features, especially when it comes to Unix systems. Therefore, learning how to incorporate them into scripts can be very beneficial and enables you to create more reliable and robust tools.

What Are Signals?

In Unix-like systems, signals are process interrupts that are delivered to a running process to notify it of specific events.

These events can be anything from you by pressing the “CTRL+ C” keys to request a process termination to other system events like system shutdown.

Some of the common signals in Unix-like systems include:

  • SIGINT (Interrupt) – This is sent when the user presses the “CTRL + C” keys. It typically causes a process to terminate gracefully.
  • SIGTERM (Terminate) – This is sent to request a process to terminate gracefully. It’s often used by the system during shutdown.
  • SIGHUP (Hang-Up) – Originally intended to notify a process that its controlling terminal has disconnected, it’s now often used for reconfiguration purposes.
  • SIGKILL (Kill) – This signal is used to forcefully terminate a process. It cannot be caught or ignored by the process.
  • SIGUSR1 and SIGUSR2 (User-Defined Signals) – These are signals that can be used by a process for custom purposes.

Now that we understand what signals are and the various types of signals in Unix, let’s move on to traps.

What Are Traps?

In shell scripting, a trap is a mechanism that allows you to intercept and handle the signals within the script.

We can specify a trap to execute a specific set of commands when a particular signal is received by the script.

A common use case is the cleanup actions or gracefully handling the interruptions.

We use the “trap” command to set up traps for signals. The syntax is as follows:

$ trap 'commands' signal

Where the commands represent the set of commands to be executed when the specified signal is received.

The signal specifies the signal that you wish to trap which is specified by its name or number.

Now that we know what traps are and how to use the “trap” command, let us explore some examples.

Handling CTLR + C (SIGINT)

Let us start with a simple example that demonstrates how to handle the SIGINT signal. The example code is as follows:

#!/bin/zsh
# Define a function to handle SIGINT
cleanup() {
   echo "Script interrupted. Cleaning up..."
   # Add your cleanup code here
   exit 1
}
# Set up a trap for SIGINT
trap 'cleanup' INT
# script logic
echo "Running a long process..."
sleep 10

In this case, when we press the “CTRL + C” keys, the “cleanup” function is executed and the script exits gracefully.

Conclusion

In this walkthrough, we covered the fundamentals of working with signals and traps to create robust scripts in ZSH.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list