Ubuntu – Cleaning log files under linux

kernellinuxlogssyslogUbuntu

I am using a kvm ubuntu 10.04 virtual machine to test some features of a kernel module. I grep through the /var/log files to observe the kernel messages about that module.

To start from a clean state, I remove log files containing older messages by executing rm -rf /var/log/* and then rebooting the virtual machine. However, after I reboot, the new log files still contain those old log messages. It takes about 5-6 cycles of deleting files and rebooting virtual machine to finally get rid of those log file messages.

Why is this happening?
Is there an easier way to cleanup log files?

Best Answer

The easiest way to clean the old logs is not to remove them.

The best way to clean the logs up is either logrotate as Hanan N. Pointed out or:

find /var/log -type f -exec /bin/cp /dev/null {} \;

for each of the log files. I am not exactly sure which log entries you are referring to from the previous boot but if you want to have this cleared after the boot you can put the command above into /etc/rc.local script so all the logging is for after the boot.

Normally though you don't want to clear the logs away since they are your starting point in all troubleshooting.

Related Question