Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait till a particular line appears in a file

Tags:

bash

shell

unix

Is it possible to write a script that does not proceed till a given line appears in a particular file?

For example I want to do something like this:

CANARY_LINE='Server started'
FILE='/var/logs/deployment.log'

echo 'Waiting for server to start'
.... watch $FILE for $CANARY_LINE ...
echo 'Server started'

Basically, a shell script that watches a file for line (or regex).

like image 795
pathikrit Avatar asked Sep 21 '14 13:09

pathikrit


2 Answers

tail -f path_to_my_log_file.log | sed '/particular_line/ q'
like image 99
Miloš Stanković Avatar answered Sep 27 '22 22:09

Miloš Stanković


You can use the q flag while parsing the input via sed. Then sed will interrupt tail as soon as Server started appears in /var/logs/deployment.log.

  tail -f /var/logs/deployment.log | sed '/Server started/ q'

Another way to do the same thing

  ( tail -f -n0 /var/logs/deployment.log & ) | grep -q "Server Started"

Previous answer (works but not as efficient than this one)

We have to be careful with loops.

For example if you want to check for a file to start an algorithm you've probably have to do something like that:

  FILE_TO_CHECK="/var/logs/deployment.log"
  LINE_TO_CONTAIN="Server started"
  SLEEP_TIME=10
  while [ $(cat FILE_TO_CHECK | grep "${LINE_TO_CONTAIN}") ]
  do
      sleep ${SLEEP_TIME}
  done

  # Start your algorithm here

But, in order to prevent an infinite loop you should add some bound:

  FILE_TO_CHECK="/var/logs/deployment.log"
  LINE_TO_CONTAIN="Server started"
  SLEEP_TIME=10
  COUNT=0
  MAX=10
  while [ $(cat FILE_TO_CHECK | grep "${LINE_TO_CONTAIN}") -a ${COUNT} -lt ${MAX} ]
  do
      sleep ${SLEEP_TIME}
      COUNT=$(($COUNT + 1))
  done

  if [ ! $(cat FILE_TO_CHECK | grep "${LINE_TO_CONTAIN}") ]
  then
    echo "Let's go, the file is containing what we want"
    # Start your algorithm here
  else
    echo "Timed out"
    exit 10
  fi
like image 32
Xavier S. Avatar answered Sep 28 '22 00:09

Xavier S.