后台作业失败时停止linux bash脚本
当后台作业在linux bash脚本中处于后台时,如何注意后台作业失败(通过任何信号SEGV、ABRT、KILL、INT…)后台作业失败时停止linux bash脚本,bash,error-handling,background-process,Bash,Error Handling,Background Process,当后台作业在linux bash脚本中处于后台时,如何注意后台作业失败(通过任何信号SEGV、ABRT、KILL、INT…) set -e foo & foo_pid=$! # Do tasks that requires that foo is running wait $foo_pid 不起作用 那会有帮助的 set -e foo & foo_pid=$! # If this script is killed, kill the `cp'. trap "kill $foo
set -e
foo &
foo_pid=$!
# Do tasks that requires that foo is running
wait $foo_pid
不起作用 那会有帮助的
set -e
foo &
foo_pid=$!
# If this script is killed, kill the `cp'.
trap "kill $foo_pid 2> /dev/null" EXIT
while kill -0 $foo_pid 2> /dev/null; do
# Do smth
...
sleep 1
done
尝试捕捉信号并更新到带有时间和日期的文件
set -e
act() {
echo "Caught signal!" > /tmp/file.txt
echo `date` >> /tmp/file.txt
exit
}
trap act SIGINT SIGTERM SIGKILL
foo &
foo_pid=$!
# Do tasks that requires that foo is running
wait $foo_pid
那么您基本上检查每个命令的进程是否仍然存在?如果一个命令要求它存在,你会用这个循环来包装它?问题是陷阱没有被触发。