I want to trap a signal send from Script-A.sh to Script-B.sh so in Script-A.sh i use the command
(Send SIGINT to Script-B.sh)
kill -2 $PID_Script-B.sh
And in Script-B.sh i catch the signal and call function Clean
trap 'Clean' 2
It does not work, instead the Script-B.sh is killed right away without performing the Clean !!
What i notice also is that if i want to send SIGINT from terminal to any script that traps it, a ctrl-c
will be catched correctly, but not if i specify the signal via the command kill -2 $pid_of_script
Any idea about the difference between the two method to send the SIGINT (ctrl-c
VS kill -2 $pid_of_script
), and how i can send a SIGINT from a script to another ?
Regards,
Debugger
In script A: Trap function will look like following which will call trap_mesg() function in scriptA.sh. KILL Signal (2/INTerrupt, 5/TERMinate-default). All, you have to do is to get the PID of a runing scriptB.sh process/session once scriptB.sh is called from scriptA.sh (nohup ... & will give you or use ps command)
Now, within scriptB.sh, do the same/similar but just for scriptB trap job (like calling clean).
This way, you dont have to source/call scriptB.sh within scriptA.sh as ". scriptB.sh ...."
I was able to reproduce the behavior you report. My hypothesis is that since the script is running from a non-interactive shell (as a child of a script) that
SIGINT
, which is a keyboard signal, is ignored.From
info bash
:I have found that if you
trap
andkill
using another signal such asSIGUSR1
it works.Additional information from
man bash
:and
and