Skip to content

trap command in MacOS

The trap command in MacOS is a useful tool for intercepting and responding to signals sent to a shell process. By using trap, you can catch signals such as interrupts or errors and execute specific commands in response. This allows you to customize the behavior of your shell script and ensure that it reacts appropriately to different situations. Additionally, trap can be used to clean up resources or perform other necessary tasks before exiting a script. Mastering the trap command in MacOS can help you create more robust and reliable shell scripts.

Terminal window
trap [action] [signal]
OptionDescription
actionThe action to be taken when the signal is received.
signalThe signal to trap and specify the action for.
ParameterDescription
actionThe command or code to be executed when the specified signal is received.
signalThe signal number or name to trap.
Terminal window
trap 'echo "Signal trapped"; exit 1' SIGINT

This command traps the SIGINT signal (usually generated by pressing Ctrl+C) and executes the specified command when the signal is received.

Terminal window
trap 'rm -f /tmp/my_temp_file' EXIT

This command sets up a trap to remove a temporary file when the script or shell session exits.

Terminal window
trap 'echo "An error occurred" >> error.log' ERR

This trap command logs errors to a file named error.log when an error occurs during script execution.

Terminal window
trap 'echo "Shutting down..." ; service_stop_command' SIGTERM

This command traps the SIGTERM signal to perform a graceful shutdown by executing the specified service stop command.

Terminal window
trap 'echo "Error occurred. Exiting..." ; exit 1' ERR

Use this command to trap errors and gracefully handle them by displaying an error message before exiting.

Terminal window
trap '' SIGINT

This trap command ignores the SIGINT signal, which prevents interruptions caused by pressing Ctrl+C.

Terminal window
trap 'echo "Received SIGHUP" ; custom_command' SIGHUP
trap 'echo "Received SIGUSR1" ; other_custom_command' SIGUSR1

This code block demonstrates trapping multiple signals, SIGHUP and SIGUSR1, to execute different commands when each signal is received.

Terminal window
cleanup() {
# Remove temporary files or perform cleanup tasks
}
trap cleanup EXIT

Define a cleanup function and set up a trap to automatically execute this function when the script or shell session exits, ensuring resources are properly cleaned up.

To use the trap command in MacOS, execute the following command:

Terminal window
trap --option <value>

What is the purpose of the trap command in MacOS?

Section titled “What is the purpose of the trap command in MacOS?”

The trap command in MacOS is used to catch and handle signals sent to the current shell or script.

Terminal window
trap 'echo "Signal received"' INT

You can trap multiple signals in MacOS by using the trap command for each signal you want to handle.

Terminal window
trap 'echo "Signal 1 received"' INT
trap 'echo "Signal 2 received"' TERM

How do I ignore a signal using trap in MacOS?

Section titled “How do I ignore a signal using trap in MacOS?”

To ignore a specific signal in MacOS using trap, you can set the action for that signal to ignore.

Terminal window
trap '' INT

To reset a trap in MacOS to its default behavior, you can use the - option followed by the signal you want to reset.

Terminal window
trap - INT

Yes, you can remove a trap in MacOS by using the - option followed by the signal you want to remove the trap for.

Terminal window
trap - INT

You can display all the traps set in MacOS by using the trap -l command.

Terminal window
trap -l

How do I set a trap command with a specific action in MacOS?

Section titled “How do I set a trap command with a specific action in MacOS?”

To set a trap command with a specific action for a signal in MacOS, specify the action to be taken within single quotes after the signal.

Terminal window
trap 'echo "Custom action"' INT
  • Capturing and handling signals
  • Executing specific actions upon receiving signals
  • Cleaning up resources or performing certain tasks before exiting
  • Customizing behavior in response to signals
  • Gracefully handling interrupts or errors