在脚本 A 中:陷阱函数将如下所示,它将调用 scriptA.sh 中的 trap_mesg() 函数。KILL 信号(2/INTerrupt,5/TERMinate-默认)。你所要做的就是在从 scriptA.sh 调用 scriptB.sh 后获取正在运行的 scriptB.sh 进程/会话的 PID(nohup ... & 会给你或使用 ps 命令)
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
##################################
现在,在 scriptB.sh 中,执行相同/类似的操作,但仅针对 scriptB 陷阱作业(例如调用 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
##################################
这样,您不必将 scriptA.sh 中的 scriptB.sh 作为“.scriptB.sh ....”进行源/调用