I want to write a shell script which will find the occurence of multiple strings like “Errors|Notice|Warnings” from a given log file, such as /var/log/messages. If any string matches it should send a mail notification to specified mail ID.
I can use:
grep -i -E '^Errors|Notice|Warnings' /var/log/messages
But my main problem is, the log file always growing, and if I want to add this script in cron, how can I record the file line or contents that I had already checked on the last execution of my script?
For example, if the log file is 100 lines and I have read the file using cat or anything similar, then before second execution, the file becomes 300 lines, then now i want to read from 101 line number to 300.
Can anyone please suggest how I can record this?
You can use following script to do that:
btw you have a problem in your regx, it should be
'Errors|Notice|Warnings'instead of'^Errors|Notice|Warnings'