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)
trap_mesg ()
{
#...do something here for script B..
# i.e.
kill -2 PID_of_ScriptB.sh_running_process_session
sleep 10; #just in case, not reqd though.
#show using ps -eAf|grep "scriptB" ... if null means, scriptB is gone. user is happy now.
#...before actually exiting out...
#show script A is exiting out as ScriptB is dead already, time for scriptA now.
#...do something here..
}
#####################################
## Trap signals : INT, TERM. catch ##
#####################################
#Set NULL/Blank value to trap_call. This variable will help in running trap_mesg only once during the life of this script.
trap_call="";
trap 'if [ -z ${trap_call} ]; then trap_call="1"; trap_mesg ; fi' 2 15
##################################
Now, within scriptB.sh, do the same/similar but just for scriptB trap job (like calling clean).
clean ()
{
echo "karoge seva to milega meva";
rm -fr /some/folder_file
}
trap_mesg ()
{
#...do something here JUST for script B trap message work..
# i.e.
clean;
#...do something here..
}
#####################################
## Trap signals : INT, TERM. catch ##
#####################################
#Set NULL/Blank value to trap_call. This variable will help in running trap_mesg only once during the life of this script.
trap_call="";
trap 'if [ -z ${trap_call} ]; then trap_call="1"; trap_mesg ; fi' 2 15
##################################
This way, you dont have to source/call scriptB.sh within scriptA.sh as ". scriptB.sh ...."