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

grep, linux, tail

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.

Problem

I need to the following things to make sure my application server is - Tail a log file for a specific string - Remain blocked until that string is printed - 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" - 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 ?

Original source