Centos – Compress logs rotated with cronolog

centoscronologlog-fileslogging

I have a bunch of servers(Centos 5.x) with Apache that have their logs rotated with cronolog. What is the best strategy for compressing and deleting those logs automatically after a certain time?
CustomLog "|/usr/sbin/cronolog /var/log/httpd/my.examplehost.com/access_log-%Y%m%d" common

I was thinking of creating a cron script that just says

gzip /var/logs/httpd/my.examplehost.com/*

But doesn't that also try to compress the file the apache is currently writing to? On cronolog homepage there is only mention that you should write your on cron jobs or similar but no instructions on how to do that.

Best Answer

Logrotate really is the tool for this job but if you can't use it then you could use find and the -ctime patameter

find /var/logs/httpd/my.example.host.com/ -ctime +0 -not -name '*.gz' -exec gzip {} \; 

should do what you want as it finds files that were changed >24hours ago that arean't already compressed and compresses them.

To ensure the file you are working on isn't still open you could do something like

#!/bin/bash
for file in $(find /var/logs/httpd/my.example.host.com/ -ctime +0 -not -name '*.gz')
do
    lsof | grep $file
    if [$? -eq 1 ]
    then
        gzip $file
    fi
done