aproxymate
Contributor
I am developing a small script which will read all logs (anything with *.log) in a folder and if it encounters a keyword (error, exception etc.), a mail will be send regarding that by writing the error in errors.txt file. This will be a demon script, running every 30 minutes. I also want to only send errors/exceptions occurred in the last 30 minutes and not before it. Below is the script:
Now I have some issues with this. The log files tend to be quite large and so I need to read only the content updated in the last 30 minutes (after the last run of the script). So I need to start grep from the last read line. I want to know if this is possible or if there is another solution to this problem?
Code:
#!/bin/sh
#create errors.txt if it does not exist
if [ ! -f ./errors.txt ];
then
touch ./errors.txt
fi
#Find log files updated in last 30 minutes in /USCRDjobs/ folder and all sub-folders
names=(`find ./log/ -iname "*.log" -type f -mmin -30`)
#Look for keywords in these files
for(( i=0 ; i< ${#names[@]} ; i++ ))
do
grep -i -m3 -H -f keywords.txt ${names[$i]} | cut -d";" -f 3,8 >> ./errors.txt
#Adding newline
echo >> ./errors.txt
done
#Mail only if errors.txt has been updated in last 30 minutes
if test `find errors.txt -mmin -30`
then
mailx -s "Alert Message" [email]admin@gmail.com[/email] < ./errors.txt
#Clear errors.txt
#> ./errors.txt
else
echo "No errors found"
fi
Now I have some issues with this. The log files tend to be quite large and so I need to read only the content updated in the last 30 minutes (after the last run of the script). So I need to start grep from the last read line. I want to know if this is possible or if there is another solution to this problem?