有没有办法检查后台进程中的脚本是否崩溃?

有没有办法检查后台进程中的脚本是否崩溃?

我正在使用 Ubuntu 20.04,我想编写一个脚本来检查 ffmpeg 命令是否在后台崩溃。崩溃发生后,应该重新初始化该命令。谁能提供 bash 脚本代码吗?

答案1

以下内容适用于任何命令,而不仅仅是ffmpeg.

最简单的情况

: &
wait -n "$!"
echo "Status was ${?}."

false &
wait -n "$!"
echo "Status was ${?}."

(exit 17;) &
wait -n "$!"
echo "Status was ${?}."

多个并行命令

declare -i i
declare -ai pids=()

for ((i = 10; i < 20; ++i)); do
  (exit "$((i))";) &
  pids+=("$!")
done

for i in "${!pids[@]}"; do
  wait -n "${pids[i]}"
  echo "Status of command Nr. ${i}, PID ${pids[i]}, was ${?}."
done

管道

false | true | (exit 11;) | true | (exit 7;) | false

declare -i pipestatus=("${PIPESTATUS[@]}")
for i in "${!pipestatus[@]}"; do
  echo "Pipeline stage ${i} terminated with status ${pipestatus[i]}."
done

相关内容