Bash Wiki
Posts (Latest 30 updated) :
Read all
Contents:
  1. [Linux] Bash trap uso: Manage signals and cleanup tasks
    1. Overview
    2. Usage
    3. Common Options
    4. Common Examples
      1. Example 1: Cleanup on Exit
      2. Example 2: Handle Interrupt Signal
      3. Example 3: Multiple Signals
      4. Example 4: Error Handling
    5. Tips

[Linux] Bash trap uso: Manage signals and cleanup tasks

Overview

The trap command in Bash is used to specify commands that will be executed when the shell receives certain signals or when specific events occur. This is particularly useful for handling cleanup tasks or managing the behavior of scripts when they are interrupted.

Usage

The basic syntax of the trap command is as follows:

trap [commands] [signals]
  • commands: The commands to execute when the specified signals are received.
  • signals: The signals that will trigger the execution of the commands.

Common Options

  • SIGINT: Triggered by pressing Ctrl+C.
  • SIGTERM: The default signal sent by the kill command.
  • EXIT: Executes commands when the script exits, regardless of the reason.
  • ERR: Executes commands when a command fails (returns a non-zero status).

Common Examples

Example 1: Cleanup on Exit

To remove a temporary file when the script exits:

trap 'rm -f /tmp/mytempfile' EXIT

Example 2: Handle Interrupt Signal

To display a message and exit gracefully when the script is interrupted:

trap 'echo "Script interrupted!"; exit' SIGINT

Example 3: Multiple Signals

To handle both SIGINT and SIGTERM with the same command:

trap 'echo "Terminating..."; exit' SIGINT SIGTERM

Example 4: Error Handling

To print a message when a command fails:

trap 'echo "An error occurred!"' ERR

Tips

  • Always ensure that the commands specified in trap are simple and quick to execute, as they will run in response to signals.
  • Use trap to manage resources effectively, especially in scripts that create temporary files or open network connections.
  • Remember to test your scripts to ensure that the trap commands behave as expected under different scenarios.