Find the answer to your Linux question:
Page 2 of 2 FirstFirst 1 2
Results 11 to 14 of 14
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 ??? Code: tail ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #11
    Just Joined!
    Join Date
    Mar 2007
    Location
    Bogotá, Colombia
    Posts
    46

    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 ???

    Code:
    tail -f /var/log/yourlog | while read $LINE
    do
      if [ `echo $LINE | grep -c "what you're searching"` -gt 0 ]
      then
        Do what must be done ...
      fi
    done
    I think that might actually work.

  2. #12
    Just Joined!
    Join Date
    Feb 2009
    Posts
    5
    BINGO! LSalab, that did it. Nice work. (Although the mail part is not working for me...).

    Code:
    #!/bin/bash
    
    EMAILADDRESS="root@athlon.com"
    EMAILSUBJECT="***** You've got a problem *****"
    MESSAGEBODY="/home/tommy/alert.txt"
    SEARCHSTRING='search string'
    SYSLOG=/var/log/messages
    
    tail -f /var/log/messages | while read LINE
    do
     if [ `echo $LINE | grep -c "search string"` -gt 0 ]
     then
            echo $LINE > $MESSAGEBODY
            echo "syslog says...Houston, we have a problem" >> $MESSAGEBODY
            mail -s "$EMAILSUBJECT" "$EMAILADDRESS" < $MESSAGEBODY
     fi
    done

  3. #13
    Just Joined!
    Join Date
    Mar 2007
    Location
    Bogotá, Colombia
    Posts
    46
    You should try to send the email a little bit "backwards":

    Code:
    echo $MESSAGEBODY | mail -s "$EMAILSUBJECT" "$EMAILADDRESS"

  4. #14
    Just Joined!
    Join Date
    Oct 2012
    Posts
    1
    Actually, I am solving this as follows (I like one liners):

    Code:
    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
    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...

Page 2 of 2 FirstFirst 1 2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •