Hi guys,
Let me explain the logic of what I am doing.
I am writing a script that looks into a log file for ERROR statements every 5 minutes and sends out an email containing a new ERROR statement occurring since last run.
The command I am trying to use is :
diff --changed-group-format='%>' --unchanged-group-format='' /opt/scripts/server.log.tmp /opt/scripts/server.tmp
The file /opt/scripts/server.tmp is always going to contain the most upto date error messages.
The file /opt/scripts/server.log.tmp contains the errors that were captured during last run of the script.
If I run the above command manually (on command line), it works perfectly.
But when I place it in a script, it does not work.
The complete script is:
#!/bin/bash
FILTER=ERROR
EXCLUDE="isEmphemeralKeyError:false"
STRING="(ephemeral|expired)"
LIVE_FILE=/opt/app/log/server.log
TMP_FILE=/opt/scripts/server.log.tmp
TMP=/opt/scripts/server.tmp
grep "$FILTER" $LIVE_FILE | grep -v "$EXCLUDE" > $TMP
#Check if a difference is present between new and old file
if diff $TMP $TMP_FILE > /dev/null
then
#Do Nothing
echo "Error Same"
else
#Put only the new ERROR messages since last run into $TMP
diff '--changed-group-format=%>' --unchanged-group-format='' $TMP_FILE $TMP > $TMP
#Send Email
ERROR=`cat $TMP`
#echo $ERROR
echo "Error occured on `hostname`
$ERROR
* This is an auto-generated email *
" | mailx -r "noreply@company.com" -s "Error occured on `hostname`" `cat /opt/scripts/email.users`
fi
#Copy new logs to old file
cp $TMP $TMP_FILE
Is there is syntax issue in the script and the 2nd diff line ?