Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top


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.


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?

share|improve this question
Why do you need to stop the program at a specific time? Due to output buffering you may end up killing the program a long time after it outputs the text you are checking for. – stardt Mar 20 '12 at 20:33
@stardt I wanted to stop the program at a specific time so that test results were more controlled and reproducible. I have found a way around this now. I will kill the program only once manually, then test only the recovery code. All recovery attempts will start from the same point. I am testing how it recovers, not how it crashes after all :) – Paul Mar 21 '12 at 17:21
up vote 1 down vote accepted

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"
match="this is the what i want to match"

$command > "$log" 2>&1 &

while sleep 60
    if fgrep --quiet "$match" "$log"
        kill $pid
        exit 0
share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

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,


share|improve this answer

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

#!/usr/bin/env expect

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

Or an one-liner for embedding in another script:

expect -c "spawn prog -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.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .