Linux – Compress and remove logs from jboss on linux with cron

compressioncronjbosslinuxlogging

I need to compress and remove log files after some time from serwer. I have never write scripts before.
So far i have script logs.sh:

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

# gzip files last modify at least 7 days ago, and not files that are identified above and that have already been compressed
find $LOGS -mtime +7 -not -name "$INFOLOG" -not -name "$ERRORLOG" -not -name "$BOOTLOG" -not -name '*.gz' -exec gzip "{}" \;

# remove all logs older than 90 days
find $LOGS -mtime +90 -not -name "$INFOLOG" -not -name "$ERRORLOG" -not -name "$BOOTLOG" -print0 | xargs -0 -I xxx rm xxx 

and i'm using cron to do it evry 5 minutes after 1 AM from jboss-logs.sh

5 1 * * * /usr/local/jboss/server/default/logs.sh

and add cron with crontab /root/jboss-logs.sh

Is it correct? will it compress files older than 7 days and remove files older than 90? Will it works? It is safe – especially removing ?

Best Answer

Use logrotate to rotate logs.

In /etc/logrotate.d/ you will find some already installed rotate patterns.

You basically define a pattern for files to handle on a daily base.

Logrotate is being triggered by cron.daily.

Use man logrotate to learn more about the easy, line based syntax.