Alternative to tail -n -0 -F for monitoring live log file

Hello,

I have been working on script which need to generate an alert based upon live logs. If string is found then an alert mail must triggered.

tail -n -0 -F works fine to redirect the each latest line from live logs file to grep a pattern for matching but it seems to be not working on SunOS 5.10 Generic_147441-19 i86pc i386 i86pc

Can anyone have a good alternative or any suggestion on this?:confused:

tail -n -0 -F $LOG_DIR"/COMMON-ERROR.log" | while read myline; do
ERROR_CODE=$(echo $myline | awk -F ' ' '{for (i=1;i<=NF;i++){if($i ~ "^[1-1]" && length($i)==6){print $i}}}')
:
:
done

Thanks,
Ketan R

Why would you tail only the last line, since you are analyzing line by line anyway in a while read loop? Try using tail -f instead. Since the -F option is not available on Solaris, you need to check if files are rotated yourself.

What does your awk do ? It print every field that starts with the digit 1 and length is exactly 6? Why do you use ^[1-1] instead of ^1 ??

In addition to what Scrutinizer already said, on Solaris systems, you need to use /usr/xpg4/bin/awk or nawk instead of awk .

Are you sure you need an immediate mail based on "live log" analysis? What be the delivery and reaction times for this mail? Depending on it going over the internet delivery times can be tens of minutes or even more. An e-mail client would pop it from the server every interval minutes only. What if the recipent is not perched on his or her keyboard?
You should reconsider the timing of the entire alert response chain and discuss it with your clients, and either relax on the timing or consider a different alert mechanism.

well this is the script i have modified.
when i am running

tail -f opt/All/Meta/CableData/logs/meta_all/Server.log" |tail +10| egrep -i "Rebuilding free list took"

on command line and appending lines in Server.log its gives the perfect out put.
but by using below script tail -f wont redirect its out put to while loop.

#!/bin/bash
o=$IFS
IFS=$(echo -en "\n\b")
LOG_DIR="/opt/All/Meta/CableData/logs/meta_all"
while read myline; do
BIND_VAL=$(echo $myline | awk -F' ' '/Rebuilding/ {print $7}')
if [ "$(echo $myline| awk -F' ' '/Rebuilding/ {print $7}' | bc -l)" -gt 5 ];then
echo $myline | mailx -s "Alert from server for Rebuilding free list " 'KetanR@abc.com'
fi
done <$(tail -f $LOG_DIR"/Server.log" |tail +10| egrep -i "Rebuilding free list took" )
IFS=$o

Executing script in debug mode:

while read myline; do

BIND_VAL=$(echo $myline | awk -F' ' '/Rebuilding/ {print $7}')
if [ "$(echo $myline| awk -F' ' '/Rebuilding/ {print $7}' | bc -l)" -gt 5 ];then
echo $myline | mailx -s "Alert from server: $SERVER_IP for Rebuilding free list " 'Ketan.Raut@sigma-systems.com'
fi
done <$(tail -f $LOG_DIR"/DHCPServer.log" |tail +10| egrep -i "Rebuilding free list took" )
tail -f $LOG_DIR"/DHCPServer.log" |tail +10| egrep -i "Rebuilding free list took"
++ tail -f /opt/Alopa/MetaServ/CableProv/logs/metaprov_new/DHCPServer.log
++ tail +10
++ egrep -i 'Rebuilding free list took'

Any Suggestion?

Don't do $() on an endless stream. That attempts to read all the text at once and hangs waiting. Use command | while read ...

seems to be same issue ....:frowning:

#!/bin/bash
o=$IFS
IFS=$(echo -en "\n\b")
LOG_DIR="/opt/All/Meta/CableData/logs/meta_all"
tail -f $LOG_DIR"/Server.log" |tail +10| egrep -i "Rebuilding free list took" | while read myline; do
BIND_VAL=$(echo $myline | awk -F' ' '/Rebuilding/ {print $7}')
if [ "$(echo $myline| awk -F' ' '/Rebuilding/ {print $7}' | bc -l)" -gt 5 ];then
echo $myline | mailx -s "Alert from server for Rebuilding free list " 'KetanR@abc.com'
fi
done 
IFS=$o

Most of what is in your latest script seems to be an attempt to either obfuscate the intent of the script or to slow down execution of your script, or both.
Please show us the output of the following commands:

head "/opt/All/Meta/CableData/logs/meta_all//Server.log" | tee /tmp/log.$$ | od -bc
cat /tmp/log.$$
rm -f /tmp/log.$$

And show us the exact output produced by your latest script (including diagnostic messages).

(And, please use CODE tags when showing us the output.)