'Is it safe to delete catalina.out if Tomcat is stopped?

My catalina.out file has 37 GB which seems to prevent my app running correctly on a linux (Centos) server since the file takes up all my server space. I never thought that a log file can get this big. Would it be safe (I have no test server to try) to stop Tomcat (Tomcat9) and just delete catalina.out? Is a new catalina.out file created when I start Tomcat again?

Or would it be better to do

 sudo echo > catalina.out

as described here. What actually does this command do?

I need to stop the Tomcat for an update of my app from time to time anyway and I don't need to store the older log files. So what is the best way to keep catalina.out small?



Solution 1:[1]

You can safely truncate (i.e. replace the content with an empty string) the catalina.out file even if Tomcat is running, which can be done manifold:

cat > catalina.out
# or
truncate catalina.out

Deleting the file, while Tomcat is down also works, but on a running Tomcat it would have a big side effect: Tomcat will still be appending data to the deleted file and the space will not be released until Tomcat shuts down.

In order to keep catalina.out small you should:

  • remove the ConsoleHandler from logging.properties (.handlers and handlers): the data logged to the ConsoleHandler is also logged to catalina.<date>.log,
  • make sure the logging system of your applications doesn't log to the standard output/standard error, but to a properly rotated file.

Solution 2:[2]

If the catalina.out is deleted after tomcat is stopped, it will create a new catalina.out once tomcat starts again and it is totally safe. In case if you remove 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. It is not recommended.

Delete/ Move catalina.out when tomcat server is stopped will not have any problem at all.

But in critical applications running on tomcat used by public, this won`t be a good solution since there will be a downtime.

Actually you can 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  

To keep catalina.out smaller and get rid of older logs, either one of the following ways can be used:

  1. You could use the above Linux command and add it to a CronScheduler to run daily/ weekly/ monthly or yearly as preferred.

  2. Use logrotate tool in Linux. It is a log managing command-line tool in Linux. This can rotate the log files under different conditions. Particularly, we can rotate log files on a fixed duration or if the file has grown to a certain size. You can click here for more info on this.

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 Piotr P. Karwasz
Solution 2 Du-Lacoste