Reputation: 15
I have a file that contains some specified error. in want to grep error from log and if I find those specied error then only I want to receive email.
grep error log
error1
error2
error3
error4
error6
error7
specified error list(of 30 errors) for which i want to receive email
error3
error7
.....
error30
I want to receive email if error matches to specified error list like error 3 and error 7 and so on..... dont want email for the error that is not in specified error list.
Upvotes: 0
Views: 44
Reputation: 124804
Let's say you have a list of error patterns in patterns.txt
:
error3
error7
And your logs are in a file called all.log
. Then you could generate a temporary file with the errors that match your specified patterns, and if the file is non-empty, then email it to yourself:
grep -f patterns.txt all.log > /tmp/matches.log
test -s /tmp/matches.log || mailx -s 'interesting errors' [email protected] < /tmp/matches.log
You can do it without a temporary file by storing the result of grep
in a variable:
matches=$(grep -f patterns.txt all.log)
test "$matches" && echo "$matches" | mailx -s 'interesting errors' [email protected]
Upvotes: 1