'How to truncate Tomcat Catalina.out log to 3 months back

My company wants us to save logs from the past 3 months. Catalina.out is getting too large on the linux server (Red hat). How can I remove everything in the log except for the last 3 months in the ONE catalina.out file.



Solution 1:[1]

I had a requirement slightly similar to you case. This is how I tackled the requirement.

Add following Linux commands into Cron Scheduler

grep the required date stamp i.e. 2022-05-07 and write to new file

grep "2022-05-07" > 2022_05_07.log // write lines which matches the data stamp
wc -l 2022_05_07.log // outputs how many lines captured

Clear the log of catalina.out file without stopping tomcat with the command below.

sudo cat /dev/null > /opt/tomcat/apache-tomcat-9.0.37/logs/catalina.out  

Make note it is not recommended to delete the catalina.out while tomcat is running.

It will keep on logging to catalina.out which is removed already (reference of the file is hold by the tomcat) hence the space will not be released. So you will need to restart the tomcat sever to release the space.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Du-Lacoste