Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

grep

tail

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 ?

like image 573
pup784 Avatar asked Dec 21 '12 02:12

pup784


People also ask

How do you tail a log file continuously?

The tail command is fast and simple. But if you want more than just following a file (e.g., scrolling and searching), then less may be the command for you. Press Shift-F. This will take you to the end of the file, and continuously display new contents.

What does $() mean in shell script?

$() – the command substitution. ${} – the parameter substitution/variable expansion.

How do I search for a text log file in Linux?

grep is a command line tool that can search for matching text in a file, or in output from other commands. It's included by default in most Linux distributions and is also available for Windows and Mac. To perform a simple search, enter your search string followed by the file you want to search.


1 Answers

#!/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.

like image 193
jim mcnamara Avatar answered Oct 13 '22 19:10

jim mcnamara