Tom Anderson’s answer is quite good, but the kill $(cat $PIDFILE) will only happen on my system if foo terminated on its own, or through Ctrl-C. The following solution works for me
while read g
do
if [[ $g =~ bar ]]
then
kill $!
fi
done < <(
exec foo 2> >(tee /dev/tty)
)