Search code examples
linuxgreptail

Shell function to tail a log file for a specific string for a specific time


I need to the following things to make sure my application server is

  1. Tail a log file for a specific string
  2. Remain blocked until that string is printed
  3. However if the string is not printed for about 20 mins quit and throw and exception message like "Server took more that 20 mins to be up"
  4. If string is printed in the log file quit the loop and proceed.

Is there a way to include time outs in a while loop ?


Solution

  • #!/bin/bash
    tail -f logfile | grep 'certain_word' | read -t 1200 dummy_var
    [ $? -eq 0 ]  && echo 'ok'  || echo 'server not up'
    

    This reads anything written to logfile, searches for certain_word, echos ok if all is good, otherwise after waiting 1200 seconds (20 minutes) it complains.