Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Note: I am using Apache2 (on Linux), but I asked in a general sense (for Linux only) because I'd also like to know the "best" way to do accomplish this in a general (since I'm about to deploy a large site on Nginx, or Cherokee).

My log files are becoming huge after just a few weeks. I need to keep them around temporarily, but I'd like to delete entries that are older than 2 weeks, or so. Is this possible, and how do I do it?

share|improve this question
    
Are you running on linux or windows? –  Scott Pack Feb 1 '11 at 1:14
    
@packs - Linux. –  orokusaki Feb 1 '11 at 1:19

2 Answers 2

up vote 8 down vote accepted

You could use logrotate. It rotates logs according to a configuration file for a specific service. It is usually run by cron on a daily basis.

An example of a config file for apache at /etc/logrotate.d/apache2

/var/log/apache2/*_log {
 daily
 rotate 31
 missingok
 compress
 delaycompress
 sharedscripts
 postrotate
  if [ -f "`. /etc/apache2/envvars ; echo ${APACHE_PID_FILE:-/var/run/apache2.pid}`" ]; then
     /etc/init.d/apache2 reload > /dev/null
  fi
 endscript
 }

This would:

  • rotate logs everyday
  • keep 31 rotated log files
  • Compress rotated logs but keep latest rotated one uncompressed (delaycompress)
  • Reload the process

If you dont want the process to be reloaded, then you should use copytruncate, which will copy the current content onto a new file, compress it and then truncate current logfile. In this case you dont need sharedscripts, postrotate and endscript.

share|improve this answer
    
In no way logrotate is a daemon, neither simpla nor complex one. :) "Normally, logrotate is run as a daily cron job" (c) manual –  poige Feb 1 '11 at 2:34
    
That is correct. Dont know what went through my mind. Fixed. –  Torian Feb 1 '11 at 2:44
    
Sample cron command? –  This_Is_Fun Feb 1 '11 at 7:33
    
EDIT: Never mind is see that is a filename not a dir... –  This_Is_Fun Feb 1 '11 at 7:38
    
You can also give logrotate a 'size' parameter. It will then only rotate that log when the file is larger than the given filesize. It's useful for lesser used files, that you may not need/want to rotate every day. –  Alister Bulman Feb 20 '11 at 23:05

The canonical tool for handling logs is logrotate.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.