How can I kill a process when a specific string is seen on standard error?

后端 未结 5 1740
执念已碎
执念已碎 2021-01-05 00:01

I need to start a process, lets say foo. I would like to see the stdout/stderr as normal, but grep the stderr for string bar. Once

5条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-05 00:19

    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)
    )
    

提交回复
热议问题