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.