How to clear catalina.out without disabling further logging?

The traditional way is to

cat /dev/null > catalina.out

It will clear the log file, and will not disrupt the processes that currently hold open file handles.

The better way is to not lose your logging information, by rotating out the log file. To do this, create or edit the file /etc/logrotate.d/tomcat and have its contents read

/var/log/tomcat/catalina.out {   copytruncate   daily   rotate 7   compress   missingok   size 5M  }

Then restart logrotate with the command (as root)

/usr/sbin/logrotate /etc/logrotate.conf

And you should have the log file rotated out daily, or if the size exceeds 5M, with the last seven logs kept for debugging purposes.


What you are looking for is log rotation. Since this requires a low-level signal support while the process is running, you should do this indirectly. You can use this procedure to achieve this. This is also documented on Tomcat Wiki.


Easy as cake: echo > catalina.out. The file descriptor won't change and java can continue to write to that file.


You can truncate the file. This makes logical sense also since it is essentially what you are trying to do.

truncate -s 0 M catalina.out

FYI: Doing a cat /dev/null > file does not alter the inode of the file.

logs]$ls -i test.log
19794063 test.log
logs]$
logs]$cat /dev/null > test.log
logs]$ls -i test.log
19794063 test.log

Also, I had a separate command tailing live data into test.log during these commands. After running these commands the tail to test.log still worked without a problem. This does not answer your question as to why it stopped working, but it helps rule out change in inode.

Tags:

Java

Tomcat