LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-28-2013, 11:42 AM   #1
socalheel
Member
 
Registered: Oct 2012
Location: Raleigh, NC
Distribution: CentOS / RHEL
Posts: 135

Rep: Reputation: 3
bash script to continuously read log file


i need to develop a bash script to continually read a log file and if a certain string exists, then notify via email.

what i have so far is below, can someone help guide me on this?

Code:
#!/bin/bash

string=fail

while true
do
if tail -f /path/to/file | grep $string
then
echo -e "String found on $HOSTNAME" | mail -s "Subject" dummy@iam.com
fi
done
 
Old 08-28-2013, 11:55 AM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 981
Blog Entries: 2

Rep: Reputation: 235Reputation: 235Reputation: 235
Your problem here is "tail -f" won't finish and the things you want won't happen.

Do this in Perl using select() to test when there is new data to read.
 
Old 08-28-2013, 12:17 PM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Have a look at this:
Code:
#!/bin/bash

string=fail

tail -n0 -F /pat/to/file | \
while read LINE
do
  if echo "$LINE" | grep "$string" 1>/dev/null 2>&1
  then
    echo "String found on $HOSTNAME" | mail -s "Subject" dummy@iam.com
  fi
done
The bold part suppresses any output to screen (stdout and stderr), edit if you want to see either/both on screen.
 
1 members found this post helpful.
Old 08-28-2013, 01:42 PM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,654

Rep: Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255
Another option is to put an entry in the syslog.conf file to invoke the script when entries appear. The advantage this has is that it will allow you to exclude some messages (such as boot, kernel messages, mail...) and only handle the class of message you need. (The section on named pipes).

You can also check the section on "Shell execute", which allows the program executed to receive the message as a single parameter. The advantage this one has is that the selected messages will be the only thing passed, and it simplifies the handling (no tail -f problems due to buffering) - Each message is sent independently of the others. The script devolves into the fragment:

Code:
if echo "$1" | grep "$string" 1>/dev/null 2>&1
  then
     echo "String found on $HOSTNAME" | mail -s "Subject" dummy@iam.com
fi

Last edited by jpollard; 08-28-2013 at 01:45 PM.
 
1 members found this post helpful.
Old 08-28-2013, 02:19 PM   #5
socalheel
Member
 
Registered: Oct 2012
Location: Raleigh, NC
Distribution: CentOS / RHEL
Posts: 135

Original Poster
Rep: Reputation: 3
thanks druuna and jpollard for your input. i've got the solution and verified it to work.

Code:
#!/bin/bash

string="fail"

tail -n 0 -F /path/to/file | \
while read LINE
do
echo "$LINE" | grep -q $string
if [ $? = 0 ]
then
echo -e "$string found on $HOSTNAME" | mail -s "Something's wrong on $(hostname)" whomever@wherever.com
fi
done
the one problem i had with that script was when i run it, it continued to stay in a loop and would never give me a [root@hostname]# prompt. i got around that by a nohup startup
Code:
nohup ./script.name.sh 0<&- &>/dev/null &

Last edited by socalheel; 08-28-2013 at 02:21 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
need a script to continuously monitor appended data to a log file akitty Programming 5 10-14-2010 03:39 AM
Help w/ script to read file and parse log message shyork2001 Linux - General 4 04-06-2010 12:48 PM
Shell script to Continuously scan a log file for success novice82 Linux - Newbie 4 10-09-2009 03:20 AM
how to log everything from a bash script to a file prodsac Linux - Server 2 04-15-2008 05:56 PM
Script to read a log file?? helptonewbie Programming 11 11-17-2007 09:18 PM


All times are GMT -5. The time now is 08:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration