23

Background:

I am writing a test script for a piece of computational biology software. The software I am testing can take days or even weeks to run, so it has a recover functionality built in, in the case of system crashes or power failures.

I am trying to figure out how to test the recovery system. Specifically, I can't figure out a way to "crash" the program in a controlled manner. I was thinking of somehow timing a SIGKILL instruction to run after some amount of time. This is probably not ideal, as the test case isn't guaranteed to run the same speed every time (it runs in a shared environment), so comparing the logs to desired output would be difficult.

This software DOES print a line for each section of analysis it completes.

Question:

I was wondering if there was a good/elegant way (in a shell script) to capture output from a program and then kill the program when a given line/# of lines is output by the program?

fixer1234
  • 28,064
Paul
  • 333

5 Answers5

16

If your program will terminate on SIGPIPE (which is the default action) it should be enough to pipe the output into a reader that will exit on reading that line.

So it might be as simple as

$ program | sed -e '/Suitable text from the line/q'

If you want to suppress the default output use

$ program | sed -n -e '/Suitable text from the line/q'

Likewise if one wants to stop after a certain number of lines one could use head in place of sed, e.g.

$ program | head -n$NUMBER_OF_LINES_TO_STOP_AFTER

The exact time of at which the kill occurs does depend on the buffering behavior of the terminal as stardt suggests in the comments.

KGardevoir
  • 103
  • 3
13

A wrapper script like this one is a standard approach. The script runs the program in the background and then loops, checking the log file every minute for some string. If the string is found then the background program is killed and the script quits.

command="prog -foo -whatever"
log="prog.log"
match="this is the what i want to match"

$command > "$log" 2>&1 &
pid=$!

while sleep 60
do
    if fgrep --quiet "$match" "$log"
    then
        kill $pid
        exit 0
    fi
done
Kyle Jones
  • 6,364
8

As an alternative to dmckee's answer, the grep command with the -m option (see e.g. this man page) command can also be used:

compbio | grep -m 1 "Text to match"

to stop when 1 line matching the text is found or

compbio | grep -v -m 10 "Text to match"

to wait for 10 lines that do not match the given text.

stardt
  • 263
3

You can use expect(1) to watch a program's stdout then execute predefined action on some patterns.

#!/usr/bin/env expect

spawn your-program -foo -bar 
expect "I want this text" { close }

Or an one-liner for embedding in another script:

expect -c "spawn your-program -foo -bar; expect \"I want this text\" { close }"

Note that expect command doesn't come with every OS by default. You may need to install it.

Jamesits
  • 141
2

You could use a "while" statement :

You need to pipe the output of your software (or of its logs) and then, for each new line, make a conditionnal test, then run kill -KILL. For example (I tested with /var/log/messages as the logfile) :

tail -f /var/log/messages | while read line; do test "$line" = "THE LINE YOU WANT TO MATCH" && kill PIDTOKILL; done

Or with the software directly :

./biosoftware | while read line; do test "$line" = "THE LINE YOU WANT TO MATCH" && kill PIDTOKILL; done

You need to remplace the log path/application name, the line to match and the PID to kill (you can also use killall).

Good luck with your software,

Hugo,

pistache
  • 143