Bash Signal Handling

Bash Signal Handling

What are signals and how to handle them in Bash

Bash scripts can receive signals from the operating system or other processes. These signals can interrupt the script and cause it to exit unexpectedly.

The trap command allows you to catch signals and handle them gracefully in your Bash scripts. This makes the scripts more robust and able to deal with interruptions.

Some common signals are:

  • SIGINT - Sent when the user presses Ctrl+C

  • SIGTERM - Sent when the script is terminated

  • SIGQUIT - Sent when the user presses Ctrl+\

To catch a signal, you use the trap command like this:

trap 'signal_handling_function' SIGINT

This will call the signal_handling_function() when the script receives the SIGINT signal (Ctrl+C).

You can define the signal handling function like this:

signal_handling_function() {
  echo "Script interrupted!"

This will print a message when Ctrl+C is pressed.

You can also use trap to ignore a signal:

trap "" SIGINT

This will ignore the SIGINT signal and the script will not be interrupted by Ctrl+C.

So in summary, the trap command allows you to define handlers for signals your Bash script receives, making it more robust and able to gracefully handle interruptions.


Here are some use cases where signal handling is useful in Bash scripts:

  • Long-running scripts: Scripts that run for a long time, perform some repetitive tasks and benefit from signal handling. It allows the user to interrupt the script gracefully instead of being forced to quit.

  • Daemon scripts: Scripts that are meant to run in the background as daemons also need signal handling. They need to catch signals to know when to shut down properly.

  • Scripts that interact with the terminal: Scripts that read from the terminal or print to the terminal should trap SIGINT (Ctrl+C) to handle the interrupt gracefully.

  • Scripts that manage processes: Scripts that spawn child processes should trap SIGTERM and SIGINT and kill any child processes before exiting. This ensures a clean shutdown.

  • Scripts that manage system resources: Scripts that manage things like file descriptors, locks, etc. should release those resources when they receive a shutdown signal.

Best practices:

  • Always trap SIGINT (Ctrl+C) to print a message and exit cleanly.

  • Trap SIGTERM to perform cleanup and exit.

  • Release system resources and kill child processes in the signal handler.

  • Log a message indicating the received signal, to make debugging easier.

  • Ignore signals that you do not need to handle, to avoid unexpected behavior.

  • Test your signal handlers by sending signals manually using kill -SIGINT <pid>

In summary, any Bash script that runs for a long time, manages system resources or child processes or interacts with the terminal, should implement proper signal handling using the trap command. This makes the script more robust and able to shut down gracefully when interrupted.

Disclaim: Created by Rix (an AI bot).