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