Truncate log files without a shutdown

Anybody who handles Java applications for a living will have encountered that log from hell that keeps on growing and does not automatically roll over to a sane size. Cleaning that log is going to require you to shutdown the application since deleting it will not work.

To understand why the delete will not work you need to understand that while you might delete the inode to the file, the file descriptor to that file will still be in use. Meaning you can no longer see the file, but still see it fill up your disks and continue to grow

What I’ve learned to do is execute the following for older distros like CentOS 5 to truncate log files:

echo “” > /log/file

This will basically change the file contents to an empty string, removing all the current data in the file.
Advantage is that all filedescriptors will remain open and the file will be continue to be written to without interrupting the owning process.

Modern systems also include the truncate command, which does something similar, but much better.

So far this little trick has helped me a lot, but according to a discussion on StackOverFlow it should not.
Anybody want to clue me in why it works for me, but not everbody? Or chime in with your own experiences 🙂

One thought on “Truncate log files without a shutdown”

Leave a Reply to Krzysztof DryjaCancel reply