Stopping tail -f when certain string found?

Hi,

I need to monitor a log file for a certain string ("Phase 2 ended") which indicates that the job which creates the log file has finished loading its data file. Once the string "Phase 2 ended" is found in the log file I would then like to stop checking that log and check to see if another logfile has been started - and start the whole process over.

Here's what I have so far ...

#!/bin/ksh
 
# Get name of the newest logfile
LogFile=`ls -tr /path/logfilename_* | tail -1`
 
if grep 'Phase 2 ended' $LogFile
then
  echo ----------
  echo "File has finished processing. No file currently being loaded."
  echo ----------
else 
  echo 
  echo ----------
  echo "File still being loaded. Will monitor progress, watch for \"Phase 2 ended\" message."
  echo ----------

  #Show all 'Phase' messages in log file so far...
  grep 'Phase' $LogFile

  #Keep checking so 'Phase 2 ended' message can be seen...
  tail -f $VAULogFile | grep Phase
fi

Now, of course, the above does not exit when it reaches 'Phase 2 ended' or the end of the file - it just sits there, in the way tail -f does.

I think I need a while read, so I can stop the tail -f and then loop to the next log file (if one yet exists) but I don't know how to do it.

Could one of you experts please point me in the right direction?

Many thanks

--
Jake

You need to grab the PID of the tail proc and kill it

if string_found
get PID && kill $PID

and then loop round

Sorry no code but hope that points you in a direction that helps.

Try:

  tail -f $VAULogFile | awk '/Phase/;/Phase 2 ended/ { exit }'

No need for a kill, the tail should terminate automatically when the pipe is closed.

That worked like a charm. Thank you very much, sir.

Thanks also, Chr15. I didn't really want to resort to a kill if possible but I appreciate your help. Thanks.