Kill program after it outputs a given line, from a shell script
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.
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
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.