-3

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?

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
  • You have been fairly vague about the details. Do the individual tests return error code, if so then why not have the framework stop at the first non-zero error code? If not, then the framework could do the output checking before continuing with the following test. – Mansoor Sep 19 '19 at 16:02
  • Edited. The individual tests to not return anyhing, they run within JVM. But from the perspective of those who start it, it doesn't matter. And yes, the framework could do a lot of things, but it's a bit hard to persuade the author of it to change/implement it... That's why I am looking for this not-so-ellegant solution – Ondra Žižka Sep 19 '19 at 16:06
  • 1
    [This answer](https://superuser.com/questions/270529/monitoring-a-file-until-a-string-is-found) seems to provide what you need. – gustgr Sep 19 '19 at 16:09
  • You can simply read the the output line by line, look for the error message pattern and stop reading when the string appears -- that should interrupt the writing process. I see two potential problems: (1) The test's output may be buffered so that 4k text are output before the read sees it -- try [unbuffer](https://linux.die.net/man/1/unbuffer). (2) the JVM may notice even later that the pipe is broken. You can perhaps capture the JVM's PID and kill it explicitly. – Peter - Reinstate Monica Sep 19 '19 at 17:57
  • Use tee for your log file and a script that uses grep to detect the string. Any output from grep should kill the PID of the top-level process. That could be done by a python script that is waiting for any line on stdin. – stark Sep 19 '19 at 21:05

1 Answers1

0

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.

Lluís Suñol
  • 3,466
  • 3
  • 22
  • 33