Automatically restart ffmpeg on error from inside bash script I use ffmpeg to livestream an Axis camera feed to youtube livestream. I have a beefy server with 32 GB of ram and plenty of CPUs doing the processing, but still the stream errors out from time to time.

Would it be possible to change my bash script so it restarts automatically on error? 

This is my ffmpeg command, and I run it from a screen session:

ffmpeg -re -thread_queue_size 512 -rtsp_transport tcp -i "${source_url}" \
   -acodec libmp3lame  -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 \
   -i /dev/zero -vcodec libx264 -preset medium -s 1280x720 -g 6 -r 3    \
   -f flv "rtmp://a.rtmp.youtube.com/live2/${youtube_stream_key}"


These are some of the error messages which cause the encoding to error and stop:

Failed to update header with correct duration.
Failed to update header with correct filesize.


this error is rare since I raised the thread_queue_size parameter:

Thread message queue blocking; consider raising the thread_queue_size option


I am looking for answers on how to make ffmpeg automatically restart.
I found this method on serverfault.com and it seems to restart ffmpeg on error:

cmd="ffmpeg -re -thread_queue_size 512 -rtsp_transport tcp -i ${source_url} \
   -acodec libmp3lame  -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 \
   -i /dev/zero -vcodec libx264 -preset medium -s 1280x720 -g 6 -r 3    \
   -f flv rtmp://a.rtmp.youtube.com/live2/${youtube_stream_key}"

until $cmd ; do
        echo "restarting ffmpeg command..."
        sleep 2
done


without " " it is showing error: -re: not found