Automatic JBoss log compression using Linux

JBoss logs can take up gigabytes of space. The logrotation works well, however, there is no (not that I could find) means to compress in real-time. However, with a little work, this may be accomplished with a cron job.

I am not going to go into too much detail or background, there are many sites that do this well. However, I will source the sites that inspired this effort.

This is tested on CentOS 5.5 using JBoss 5.1.0 GA

I want to automate the compression of JBoss logs, then move these logs to another location after a period of time has passed.

A minor modification has been made to the script to account for files that had already been compressed with the extension of gz. This modification eliminates any error messages related to files that have already been compressed. The old line of code still exists and has been commented out for comparison. The new code is a little cleaner, too.

Create the script.

gedit jboss-logs.sh

Copy the following into the jboss-logs.sh, modify where necessary.

#!/bin/bash
LOGS=/usr/local/jboss/server/default/log/
LOGBACK=/usr/local/jboss/server/default/backup/
INFOLOG="server.log"
ERRORLOG="error.log"
BOOTLOG="boot.log"

# gzip files last modify at least 1 hour ago, and not files that are identified above and that have already been compressed
#find $LOGS -mmin +61 -not -name "$INFOLOG" -not -name "$ERRORLOG" -not -name "$BOOTLOG" | xargs gzip -f
find $LOGS -mmin +61 -not -name "$INFOLOG" -not -name "$ERRORLOG" -not -name "$BOOTLOG" -not -name '*.gz' -exec gzip "{}" \;

# move all logs older than 15 days
find $LOGS -mtime +15 -not -name "$INFOLOG" -not -name "$ERRORLOG" -not -name "$BOOTLOG" -print0 | xargs -0 -I xxx mv xxx $LOGBACK

Save the script, then make it executable.

chmod +x jboss-logs.sh

The script can manually be executed.

./jboss-logs.sh

Or, create a cron job to automate the process. I wanted a cron job to run at 12:05 AM everyday. To create a cron job, create a text file.

gedit jboss-logs.txt

Copy the following into it and make sure to press ENTER to start a new line:

5 0 * * * /usr/local/jboss/server/default/backup/logs.sh

jboss-logs

Save the file. Then create a cron job.

crontab /root/jboss-logs.txt

It can be verified to exist with the following command.

crontab -l

crontab-e

Done. The result should be an automated task to compress JBoss logs and move then after so many days to another location at 12:05 AM.

Resources
What is a cronjob, and how do I use it?
Excluding files in FIND results
xargs
How do I add jobs to cron under Linux or UNIX oses?
find
How to gzip all files in all sub-directories in bash