I am using Ubuntu 20.04 and I want to write a script that checks if ffmpeg command crashed in the background. After the crash happened it should reinitialize that command. Can anyone provide a bash script code for that?

  • Type "jobs" to see the background jobs. Aug 26, 2021 at 4:42
  • But then, you'll have to write a script that will check that the script you wrote to check if the script didn't crashed didn't crashed itself .... ;-)
    – binarym
    Aug 26, 2021 at 9:09
  • @binarym yeah that's another problem Aug 27, 2021 at 14:47

1 Answer 1


The following applies to any command, not just ffmpeg.

The simplest case

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

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

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

Multiple parallel commands

declare -i i
declare -ai pids=()

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

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


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]}."
  • So if my ffmpeg crashes will one of this reinitialize it again? @Andrej Podzimek Aug 28, 2021 at 17:32
  • No, you have to reinitialize it once you detect a failure. So instead of a trivial command, you could run a retry loop in the background, like for ((i = 0; i < 10; ++i)); do ffmpeg ... && break; done &. That would retry immediately after a crash, but not indefinitely. Ultimately, when retries don’t help, the user needs to sort out the situation anyway. Aug 29, 2021 at 18:11
  • I don't think that code is what I exactly want. Because I want to check the ffmpeg's situation even after it is successfully executed. But I am gonna try the others thanks for the help Aug 30, 2021 at 20:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .