I have written below script and it reads the file and when ever there is any 400* or 500* word it will give me error message, and now I want to add two more functionality to my script.

  1. I added the script in cronjob to run after 5mins , but now I want the script to continuously run and monitor the log file as it is updating and send me error as per my below requirement.

  2. Rather than checking each line in the file fore 400*, 500* , I want the script to check ""GET / HTTP/1.1" 200" in each line and whenever it is not in the line, it should give me an error message.

find /home/ -type f -name "apache_logs.log" |while read file
    RESULT=$(egrep "[^0](400|401|403|404|405|406|407|408|409|410|411|412|413|414|415|416|417|418|422|425|426|428|429|431|451|500|501|502|503|504|505|511)" $file)
      if [[ ! -z $RESULT ]]
            echo "Error(s) in $file on $HOSTNAME at "$(date)": $RESULT">> result.txt
  • grep -v will inverse matching for an expression; you can use grep -v '"GET / HTTP/1\.1" 200' to find any lines which do not match that pattern, and act upon them. – DopeGhoti Jun 13 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.