How to clear Tomcat catalina.out without stopping it on Linux

In case when your installation has enabled debug/trace logs catalina.out file may overgrow very quickly.
Apache Tomcat is sensible to change of file descriptor of file catalina.out. So if catalina.out file has been moved or deleted Tomcat may stop working. To prevent tomcat restart and save logs you may use this solution.

Instructions

  1. Run next bash command cp catalina.out catalina.out.2020-12-03 && cat /dev/null > catalina.out this will create copy of catalina.out file and then erase it.

  2. To compress backup file use for example gzip, run next command: gzip catalina.out.2020-12-03

 

One thing to note that you must have enough of space on disk to create a copy of catalina.out.

In case when no space left on device

and there is no way to add more space, you can drop old logs from beginning of catalina.out file by using command which works with xfs and ext4 file systems :

#delete the first 1GB: fallocate -c -o 0 -l 1G catalina.out