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

后端 未结 5 1738
执念已碎
执念已碎 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:23

    Use Expect to Monitor Standard Error

    Expect is designed for taking actions based on output from a process. The simplest solution is to simply let Expect start the process, then exit when it sees the expected output. For example:

    expect -c 'set msg {Saw "foo" on stderr. Exiting process.}
               spawn /bin/bash -c "echo foo >&2; sleep 10"
               expect "foo" { puts $msg; exit }'
    

    If the spawned process ends normally (e.g. before "foo" is seen), then the Expect script will exit, too.

提交回复
热议问题