Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

grep

bash

stderr

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 bar is found in the stderr foo should be killed.

Is this possible?

like image 215
Zombo Avatar asked Dec 30 '12 19:12

Zombo


1 Answers

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.

like image 188
Todd A. Jacobs Avatar answered Sep 24 '22 02:09

Todd A. Jacobs