0

I have a crontab script which monitor a DR process between 2 machines and this script generate a log file. What i have been asked to do is basically append the new log generated on the top of the previous (as log i use the same file name) and not on the bottom.

I've seen already few options, but all my tentative failed.

I tried with

cat $LOGFILE >> $TEMPLOG
rm $LOGFILE
mv -i $TEMPLOG $LOGFILE

and also with

cat - $LOGFILE > $TEMPLOG && mv $TEMPLOG $LOGFILE

the variable $LOGFILE is where the script append every single statement of the process.

Thanks :)

Basically want I'd like to to do is generate the right log with on top the last run before it is sent by mail.

DATE=`date "+%d%m%y_%H%M"`
PRIMARY_HOSTNAME=`hostname`
LOGFILE=/dba/logs/monitor_sync_FM2.log
TEMPLOG=/dba/logs/monitor_sync_LOG.log
SERVER=`hostname`
SITE=mycompany
EMAILTO="email@company.com"
DBOPS="oracle@${SERVER}.${SITE}"

export PRIMARY_HOSTNAME LOGFILE TEMPLOG SERVER SITE EMAILTO DBOPS DATE
echo "\n\n### monitor DR sync  started @ `date` ###" >> $LOGFILE
echo "Running SQL command to verify latest SCN.." >> $LOGFILE
echo "The current SCN of the Primary DB server is: $PRIMARY_CURRENT_SCN" >> $LOGFILE

echo "Connecting now to the secondary standby database server..." >> $LOGFILE
SECONDARY_CURRENT_SCN=`ssh oracle@xxx.xxx.xxx.xxx /home/oracle/script_sync2.sh` >> $LOGFILE
export SECONDARY_CURRENT_SCN
echo "Secondary SCN output returned as: $SECONDARY_CURRENT_SCN" >> $LOGFILE
grep ORA- /dba/scripts/output.txt
if [ $? = 0 ]; then
echo "Remote ssh command to Secondary server failed..Exiting" >> $LOGFILE
echo "### monitor DR sync failed @ `date` ###" >> $LOGFILE
echo "PROBLEM" >> $LOGFILE

mailx -r ${DBOPS} -s "PROBLEM" ${EMAILTO} < $LOGFILE
exit
else
echo "The current SCN of the Secondary DB server is: $SECONDARY_CURRENT_SCN" >> $LOGFILE
DIFF=`expr $PRIMARY_CURRENT_SCN - $SECONDARY_CURRENT_SCN` ; export DIFF
if [ $PRIMARY_CURRENT_SCN -ne $SECONDARY_CURRENT_SCN ]; then
        echo "The difference is $DIFF" >> $LOGFILE
        if [ `echo $DIFF` -gt 3 ]; then
                echo "Log Gap: $DIFF" >> $LOGFILE
                                echo "PROBLEM" >> $LOGFILE
                                echo "### script finished @ `date` ###\n\n" >> $LOGFILE
                mailx -r ${DBOPS} -s "PROBLEM" ${EMAILTO}  < ${LOGFILE}
        else
                                echo "SUCCESS" >> $LOGFILE
                mailx -r ${DBOPS} -s "SUCCESS" ${EMAILTO}  < ${LOGFILE}
                echo "Log Gap: $DIFF" >> $LOGFILE
        fi
else
echo "Log Gap: $DIFF" >> $LOGFILE
echo "SUCCESS" >> $LOGFILE
mailx -r ${DBOPS} -s "SUCCESS" ${EMAILTO}  < ${LOGFILE}
fi
fi
  • Why does it needed to be added to the beginning of the file? – Ignacio Vazquez-Abrams Nov 6 '17 at 16:02
  • @IgnacioVazquez-Abrams Because the will be sent to a machine with a monitoring agent which check from the top of the file, so i'd like to have the last run/checking on the top – FaMontyN2 Nov 6 '17 at 16:04
  • @MarkPlotnick sorry, i don't get your comment – FaMontyN2 Nov 6 '17 at 16:05
  • Does each line of the logfile have a timestamp? If so, and you want newer portions of the logfile added at the top, does that mean that the timestamps will not all be in order? – Mark Plotnick Nov 6 '17 at 16:14
  • @MarkPlotnick Yes, there is a time stamp on the top of the portion of log generate every run. And i don't mind that won't be in order, it won't be a problem. – FaMontyN2 Nov 6 '17 at 16:16
1

If you want to "append" (insert) the current run's logs to the beginning of the existing log file, then follow through with your apparent attempt at using a TEMPLOG file. Write all of the script's output/data to that $TEMPLOG file, then do:

ed -s $LOGFILE <<< "0r $TEMPLOG"$'\n'wq

To read $TEMPLOG into $LOGFILE after line zero.

As an example:

$ cat logfile
previous
entries
here
$ cat templog
New
Entry goes
Here
$ ed -s "$LOGFILE" <<< "0r $TEMPLOG"$'\n'wq
$ cat logfile
New
Entry goes
Here
previous
entries
here

If your shell does not support here-strings, print the instructions to ed's stdin instead:

printf "0r $TEMPLOG\nwq\n" | ed -s logfile

If you only need the contents of the email to be changed, and not the $LOGFILE itself, just prepend it in the pipe to mail:

cat "$TEMPLOG" "$LOGFILE" | mailx -r ${DBOPS} -s "SUCCESS" ${EMAILTO}
  • Hi Jeff, i tried your suggestion with ed but it doesn't work: ./monitor_sync_1_FMn2.sh[41]: syntax error at line 45 : `<' unexpected Should i use a specific shell for it? – FaMontyN2 Nov 7 '17 at 9:10
  • Yes; I think bash, zsh, and ksh support it (it’s a here-string as distinguished from a here-document) – Jeff Schaller Nov 7 '17 at 10:19
  • Hi Jeff, i just tried to run that command out of the script with 2 random files in KSH but still no joy: scripts $ ed -s FM2.test <<< "0r IG.test"$'\n'wq ksh: syntax error: `<' unexpected – FaMontyN2 Nov 8 '17 at 12:02
  • Are you working with ksh v88 maybe? – Jeff Schaller Nov 8 '17 at 12:41
  • @FaMontyN2, I've added a non-here-string option to the answer; see if that works any better. – Jeff Schaller Nov 8 '17 at 15:35
0

Since your script appends to $LOGFILE, I think what you want is

mv $LOGFILE $TEMPLOG

at the beginning and

cat $TEMPLOG >>$LOGFILE

just before mailing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.