restart encoding with ffmpeg when bad source
Although I am not sure if stderr will really help here because of the way that ffmpeg does its reporting, I can recommend the following pattern.

Use the -progress [url] flag and parse it with the tool of your choice every 100ms or so. Here is something to get you started that (with a little tweaking) might do what you want:

#!/bin/bash

touch /tmp/ffmpeg.log

ffmpeg -shortest -re -y -err_detect aggressive -i \ 
rtmp://127.0.0.1/movies/my.stream timeout=10 -codec copy \
-map 0 -bsf:v h264_mp4toannexb,dump_extra -f segment \
-segment_time 60 -progress /tmp/ffmpeg.log /video/my.stream#%09d.ts &amp;

pid=$!

while true
    do
         # you'll might have to change "invalid" 
         # for whatever is being put into the progress log
         # just look at /tmp/ffmpeg.log to figure out what
         # grep should try to find 
         error=$(grep -n "invalid" /tmp/ffmpeg.log)

         # is the length of the error more than 0 / did grep find it???
         if (( ${#error} &gt; 0 ))
             then
                 kill -9 $pid
                 ffmpeg -shortest -re -y -err_detect aggressive -i \ 
                 rtmp://127.0.0.1/movies/my.stream timeout=10 -codec copy \
                 -map 0 -bsf:v h264_mp4toannexb,dump_extra -f segment \
                 -segment_time 60 -progress /tmp/ffmpeg.log /video/my.stream#%09d.ts &amp;
             pid=$!
         else
             usleep 100
         fi
    done
exit 0