Cron to clean up old log files
April 01, 2014
If you run a few Weblogic dev server installs like the ones set up with the scripts in other parts of this blog, or just dev servers in general there’s a good chance there’s old and useless log files you can get rid of. Mainly because they cause clutter and make it harder to find what you are actually looking for.
The find command with it’s set of magic arguments will be your friend in this case.
So lets layer this up
find /your/log/dir
Will return all files and directories in and under your log directory, which is probably too much
find /your/log/dir -maxdepth 1
Will keep the depth to just your log directory
We are only interested in files not directories
find /your/log/dir -maxdepth 1 -type f
Will restrict find to just files.
Now we don’t want all just the older ones
find/your/log/dir -maxdepth 1 -type f -mtime +7
Will list files modified over 7 days ago
Then to restrict to you certain log files, for example I have my weblogic access logs set up as ${serverName}_access_%yyyy%_%MM%_%dd%_%hhmm%.log
So I can find just them using
find /your/log/dir -maxdepth 1 -type f -mtime +7 -regex ".*access_[0-9]+_[0-9]+_[0-9]+_[0-9]+\.log"
Once you are happy that is is finding the files you want to get rid of it’s time to start deleting. It’s best to get the find itself right first because if you go directly to the deleting you might end up taking too much.
find /your/log/dir -maxdepth 1 -type f -mtime +7 -regex ".*access_[0-9]+_[0-9]+_[0-9]+_[0-9]+\.log" -exec rm {} \;
-exec runs the command you specify, in this case rm, {} will be replaced with the file find found ; tells exec it’s done.
Once you are happy with that it’s handy enough add them to a cron job, having individual lines means you can have different time frames for the different logs
# Remove old log files
# Remove access logs
30 01 * * * find /your/log/dir -maxdepth 1 -type f -mtime +7 -regex ".*access_[0-9]+_[0-9]+_[0-9]+_[0-9]+\.log" -exec rm {} \;
# Remove server logs
30 01 * * * find /your/log/dir -maxdepth 1 -type f -mtime +7 -regex ".*_[0-9]+_[0-9]+_[0-9]+_[0-9]+\.log" -exec rm {} \;
# Remove application logs
30 01 * * * find /your/log/dir -maxdepth 1 -type f -mtime +7 -regex ".*.log\.[0-9]+-[0-9]+-[0-9]+" -exec rm {} \;