Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to detect *which* trap signal in bash? [duplicate]

Tags:

bash

bash-trap

Possible Duplicate:
Identifying received signal name in bash shell script

When using something like trap func_trap INT TERM EXIT with:

func_trap () {     ...some commands... } 

Is there a way in the function block to detect which trap has called it?

Something like:

func_trap () {     if signal = INT; then         # do this     else         # do that     fi } 

Or do I need to write a separate function for each trap type that does something different? Is there a bash variable that holds the latest received signal?

Thanks in advance!

like image 385
Wolf Avatar asked Feb 01 '10 09:02

Wolf


People also ask

What is trap exit in bash?

The secret sauce is a pseudo-signal provided by bash, called EXIT, that you can trap; commands or functions trapped on it will execute when the script exits for any reason.

What is trap signal?

Trap allows you to catch signals and execute code when they occur. Signals are asynchronous notifications that are sent to your script when certain events occur. Most of these notifications are for events that you hope never happen, such as an invalid memory access or a bad system call.

Why we use trap command in Linux?

Description. trap defines and activates handlers to run when the shell receives signals or other special conditions. ARG is a command to be read and executed when the shell receives the signal(s) SIGNAL_SPEC.

What is trap command in Bash?

A built-in bash command that is used to execute a command when the shell receives any signal is called `trap`. When any event occurs then bash sends the notification by any signal. Many signals are available in bash. The most common signal of bash is SIGINT (Signal Interrupt).

How do I trap a signal in a shell script?

To trap a signal any time during the execution of a shell script, define a trap statement at the start of the script. Alternatively, to trap a signal only when certain command lines are to be executed, you can turn on the trap statement before the appropriate command lines and turn off the trap statement after the command lines have been executed.

What is the most common signal of Bash?

The most common signal of bash is SIGINT (Signal Interrupt). When the user presses CTRL+C to interrupt any process from the terminal then this signal is sent to notify the system. How you can use trap command to handle different types of signals is explained in this tutorial.

How do I trap a SIGTERM signal in Linux?

Set `trap` command for SIGTERM in a script SIGTERM signal is used to terminate the process immediately by releasing its resources. Create a bash file named ‘ trapscript.sh ’ with the following code. An infinite for loop is declared in the script that will print a text continuously until SIGTERM signal occurs.


2 Answers

You can implement your own trap function that automatically passes the signal to the function:

trap_with_arg() {     func="$1" ; shift     for sig ; do         trap "$func $sig" "$sig"     done }  $ trap_with_arg func_trap INT TERM EXIT 

The first argument to func_trap will be the name of the signal.

like image 67
camh Avatar answered Sep 20 '22 01:09

camh


No documentation hints of any argument or variable holding the signal that was trapped, so you'll have to write a function/trap statement for each trap you want to behave differently.

like image 36
nos Avatar answered Sep 20 '22 01:09

nos