Skip to content

Instantly share code, notes, and snippets.

@xuan9
Created December 28, 2015 10:12
Show Gist options
  • Select an option

  • Save xuan9/ebe396ec71f4370b7b7f to your computer and use it in GitHub Desktop.

Select an option

Save xuan9/ebe396ec71f4370b7b7f to your computer and use it in GitHub Desktop.
#!/bin/bash
# If Tomcat uses server.xml config to rotate localhost_access_log,
# the daily rotated logs will need compressing and old ones deleted to stop filling
# the log partiton. Cannot use the system logrotate command as conficts with tomcat rotate
# therefore run this script in a daily cronjob
#
# localhost_access_log.2015-09-03.txt
#
# Add this script in /etc/cron.daily/ owned by root
#
CATALINA_BASE=/var/opt/tomcat
cd ${CATALINA_BASE}/logs
if [ $? -ne 0 ]
then
echo "Error, cannot cd to logs directory, quitting...."
exit 1
fi
# today's date (not to be gzipped)
DATE=`date --rfc-3339=date`
# number of days to keep
MTIME=90
# Compress all previous days uncompressed logs
for log in `ls *access* | grep -v bz2 | grep -v $DATE`
do
bzip2 $log
done
# delete old logs
find . -name "*.bz2" -mtime +$MTIME -exec rm {} \;
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment