Linux: How to cat stdout of a process until the end, but stop and return a non-zero code if certain string appears?

323 views Asked by At

We have a tool that runs tests, but does return an error code if they fail.
The tool runs the tests after starting logging in through SSH to a custom console (not bash) and issuing a command. All tests run at once within that invocation The logging of the tests goes to a file. The output of the tool is roughly:

test1 [ok]
test2 Some message based on the failure
...

To stop the build, we need to look for certain strings in the output.
The output appears as the tests run.

I could capture the whole output into a file and fail at the end. But it would save quite some time to fail once the first test fails.

Therefore, I would like something like tee, but it would also kill the execution if it finds that failure string. Or, at least, it should print the output as it comes, and return non-zero if a string is found.

Is this doable with the standard Linux toolkit?

1

There are 1 answers

0
Lluís Suñol On

The only solution I can think of is:

  1. Start you build process and cat its output to an output file.
  2. Start another script that monitors this file: a loop which iterates every X seconds in search for your, lets say, forbidden words in the file. As soon as they appear, kill the build process (you may need a way to identify your build process, such as a pid file or something like this) and clear the file.

You can even put this 2 processes in a single shellscript and make them both start and stop when needed.