Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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/" common

I was thinking of creating a cron script that just says

gzip /var/logs/httpd/*

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.

share|improve this question
Is there any reason why you can't use logrotate which will do what you want natively ? – Iain Jul 27 '12 at 8:14
That's how our servers are set up with. Lots of other software rotate logs also without logrotate, for example Tomcat, so solution to this could also solve those. – palto Jul 27 '12 at 10:25
up vote 3 down vote accepted

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/ -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

for file in $(find /var/logs/httpd/ -ctime +0 -not -name '*.gz')
    lsof | grep $file
    if [$? -eq 1 ]
        gzip $file
share|improve this answer
Great, I didn't know you could set -not on the find parameters! But it seems like it isn't necessary because gzip detects files that are already compressed. I ended up modifying your script to compress files that are a week old saved it under /etc/cron.daily/ to run it every night. Works great. – palto Jul 29 '12 at 19:13

Your Answer


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.