we have a log rotation stub in a cron job as given below, say $1 is /var/tmp/abc.log, MAX_LOG_SIZE_IN_BYTE is 5000 (i.e 5 KB), MAX_LOG_COUNT is 5 which says the expectation is to append logs to existing log file and if log file size crosses 5 KB, rotate log files and thus arrange logs in new files /var/tmp/abc.log.1, /var/tmp/abc.log.2 till /var/tmp/abc.log.5, and logs over 25 KB gets flushed and always recent logs are written to /var/tmp/abc.log
function LOG_ROTATE () {
local LOG_FILE=$1
fileSizeInBytes=$(stat -c%s "$LOG_FILE")
if [ "$fileSizeInBytes" -ge "$MAX_LOG_SIZE_IN_BYTE" ]
then
# Deletes old log file
if [ -f $LOG_FILE ] ; then
CNT=$MAX_LOG_COUNT
P_CNT=$(($CNT-1))
if [ -f ${LOG_FILE}.$MAX_LOG_COUNT ] ; then
rm -f ${LOG_FILE}.$MAX_LOG_COUNT
fi
# Renames logs .1 trough .$MAX_LOG_COUNT
while [[ $CNT > 1 ]] ; do
if [ -f ${LOG_FILE}.${P_CNT} ] ; then
mv ${LOG_FILE}.${P_CNT} ${LOG_FILE}.${CNT}
fi
CNT=$(($CNT-1))
P_CNT=$(($P_CNT-1))
done
# Renames current log to .1
mv $LOG_FILE ${LOG_FILE}.1
touch $LOG_FILE
fi
fi
}
But whenever cron job is run, contents of /var/tmp/abc.log are being replaced with new logs and no rotation is taking place. Help to append logs instead of replacing and thus if old logs are preserved, rotation will also take place.