Well then, I stand corrected...
However, I think the concept still applies, the problem becomes order...
What if you do the grep after you read each line ???
I think that might actually work.
tail -f /var/log/yourlog | while read $LINE
if [ `echo $LINE | grep -c "what you're searching"` -gt 0 ]
Do what must be done ...
BINGO! LSalab, that did it. Nice work. (Although the mail part is not working for me...).
EMAILSUBJECT="***** You've got a problem *****"
tail -f /var/log/messages | while read LINE
if [ `echo $LINE | grep -c "search string"` -gt 0 ]
echo $LINE > $MESSAGEBODY
echo "syslog says...Houston, we have a problem" >> $MESSAGEBODY
mail -s "$EMAILSUBJECT" "$EMAILADDRESS" < $MESSAGEBODY
You should try to send the email a little bit "backwards":
echo $MESSAGEBODY | mail -s "$EMAILSUBJECT" "$EMAILADDRESS"
Actually, I am solving this as follows (I like one liners):
Basically, line-buffered turns off the buffer for grep, -u turns it off for sed, and you can also use stdbuf -o0 in front of anything to disable output buffering...
tail -n 1000000 -f logfile | grep --line-buffered "^MA:" | sed -u 's/.*Subject: \(.*\) Body: \(.*\)/echo \"\1: LogWatcher: \2\" | mail -s \"\1\" XXX@gmail.com/' | bash