sh -c 'echo "$$"; exec tail -f filename.log' | (
IFS= read -r pid &&
sed '/^Abaqus JOB filename COMPLETED$/q
/^Abaqus\/Analysis exited with errors$/q' &&
kill -s PIPE "$pid"
)
If we only did tail -f file.log | sed ...q, sed would quit upon seeing any of those lines, but then tail would only terminate (killed by a SIGPIPE) if ever it wrote anything to the pipe after that. And if that log file is never updated because the process that logs things to it has died, then that will never happen.
So here instead, we're sending the SIGPIPE (rather than a SIGTERM which with some shells cause an unwanted diagnostic message to be printed) manually. Using sh to pass the pid of tail (which reuses that of sh because of exec) along to the right hand side of the pipe in the beginning.
A perl equivalent:
sh -c 'echo "$$"; exec tail -f filename.log' | perl -lne '
BEGIN {$pid = <> or die "No pid."}
print;
if ($_ eq "Abaqus JOB filename COMPLETED" ||
$_ eq "Abaqus/Analysis exited with errors") {
kill "PIPE", $pid;
last;
}'
tail -fwhen you want to watch output in realtime, so why would you need to kill it automatically? – terdon Mar 01 '20 at 19:41