shell script to grep 500 error messages from access logs

Hello Team,

I need help to improve my script which is used to grep 500 error messages in the logs.

I am using following logic in the script to grep 500 error messages in the logs.

var1=`awk '$9 == "500"' access_log | tail -1`

The above logic is not useful if logs are not getting updated as it will always grep for old error messages. My requirement is it should always grep for latest 500 error messages.The below is the output from access log of apache.

kindly help to apply this logic.

If the messages in the log file are in a subsequent order, your command should work IMHO.

Post a snippet of your log file and the desired output.