Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
后台作业失败时停止linux bash脚本_Bash_Error Handling_Background Process - Fatal编程技术网

后台作业失败时停止linux bash脚本

后台作业失败时停止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

当后台作业在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_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

那么您基本上检查每个命令的进程是否仍然存在?如果一个命令要求它存在,你会用这个循环来包装它?问题是陷阱没有被触发。