mardi 9 novembre 2021

How to find and automatically remove old catalina logs

Tomcat can produce daily log files, potentially filling your disk space over time.

You can list the catalina log files old than 30 days using

find /srv/apache-tomcat-8.5.14/logs/catalina.*.log -mtime +30

To remove files older than 30 days, you can run the following command:

find /srv/apache-tomcat-8.5.14/logs/catalina.*.log -mtime +30 -exec rm -f {} \;

Finally, you can make it a cron to remove old log files daily:

crontab -e

In the cron file, add the regex and command:

# every day at 6PM
0 18 * * * find /srv/apache-tomcat-8.5.14/logs/catalina.*.log -mtime +30 -exec rm -f {} \;

You can then list all crontabs using

crontab -l

NB: if you want your command to run as sudo, you need to modify the sudo crontabs instead:

sudo crontab -e