When a signal is received, I can execute some commands using trap. Example:
trap 'echo hello world' 1 2
If any of the signals specified is received, the hello world’ is displayed.
But how can I print/identify the received signal name?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
(If you only have the number of a signal and want the name,
kill -l $SIGNAL_NUMprints the name of a signal; you can avoid that by using the signal names instead of numbers in your call totrapas below.)This answer says that there’s no way to access the signal name, but if you have a separate function for each signal that you trap, then you already know the signal name:
In many cases, that may be sufficient, but another answer on that same question uses that fact to provide a workaround to fake the behavior you want. It takes a function and a list of signals and sets a separate trap for each signal on that function called with the signal name, so internally it’s actually a separate function for each signal but it looks like a single trap on a single function that gets the signal name as an argument:
Code:
If I run that, then I can send signals to the process and I get output like