我有一个Bash父脚本,在意外的输入上调用记录错误的错误记录子脚本.我还希望在发生错误并调用错误脚本时暂停执行.但是,如果我exit从错误处理脚本调用它,它不会停止执行父脚本.我怎样才能从孩子那里停止父脚本?
jm6*_*666 11
尝试..
#normal flow
[[ $(check_error_condition ]] && /some/error_reporter.sh || exit 1
Run Code Online (Sandbox Code Playgroud)
所以,
你不想要stop the parent from a child(父母通常不喜欢这种行为):),你想要tell to parent - need stop,他会停止自己(如果想要);)
尝试:
在父脚本中:
trap "echo exitting because my child killed me.>&2;exit" SIGUSR1
Run Code Online (Sandbox Code Playgroud)
在子脚本中:
kill -SIGUSR1 `ps --pid $$ -oppid=`; exit
Run Code Online (Sandbox Code Playgroud)
其他方式是:
在子脚本中:
kill -9 `ps --pid $$ -oppid=`; exit
Run Code Online (Sandbox Code Playgroud)
但是,不建议这样做,因为父母需要有关于被杀的一些信息,因此如果需要可以进行一些清理.
另一种方式:它不是调用子脚本exec.
但是,正如其他答案所指出的那样,最简单的方法是在孩子返回后退出父母.