Clearing Logs on Ubuntu System Shutdown

I have a virtual machine of Ubuntu 10.10 that I use for development purposes. The other day, I got this message:

The volume "var" has only 0 bytes disk space remaining.

Since the box is for development purposes, I have no real need to keep several days worth of logs. So I cooked up a script to delete the logs:

#!/bin/bash

# delete archived logfiles
for i in `find /var/log -type f \( -name \*.\[0-9\] -o -name \*.bz2 -o -name \*.gz -o -name \*.old \)`
do
  rm $i
done

# clear remaining logfiles
for i in `find /var/log -type f`
do
  cat /dev/null > $i
done

I saved it as clearlogs.sh, in my /etc/init.d folder. Then, I made it executable, and created symbolic links in rc0.d and rc6.d to make it run on shutdown and restart:

chmod +x /etc/init.d/clearlogs.sh
ln -s /etc/init.d/clearlogs.sh /etc/rc0.d/K10clearlogs.sh
ln -s /etc/init.d/clearlogs.sh /etc/rc6.d/K10clearlogs.sh

Now every time I load my system, I have clean logs. I would never do this on a production system or even my primary system, but for a dev box this works great.

blog comments powered by Disqus  -  Home