Find multiple strings in a file and email on occurence of any defined string
سؤال
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:
start=0
[[ -f last-processed ]] && start=$(<last-processed)
start=$((start+1))
tail +$start /var/log/messages | grep -i -E 'Errors|Notice|Warnings' &&\
wc -l /var/log/messages | awk '{print $1}' > last-processed
btw you have a problem in your regx, it should be 'Errors|Notice|Warnings'
instead of '^Errors|Notice|Warnings'
نصائح أخرى
Rotating your log file could be the best solution.
But if you want to grep file from line_first
to line_last
you can use sed
:
For example, get line from 100 to 110 from input stream:
$> line_first=100; line_last=110
$> seq 1 1000 | sed -n "${line_first},${line_last}p"
100
101
102
103
104
105
106
107
108
109
110