O'Reilly logo

Linux Server Hacks by Rob Flickenger

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Hack #92. Rotate and compress Apache Server Logs

Use this tiny Perl script to compress all of your Apache logs automatically, even when you add more to your site

Here's a handy script that will rotate and gzip your Apache logs for you automatically. It reads your httpd.conf, descends all Included configuration files, and makes a note of each unique Log file. It then renames each to a file with a datestamp, restarts Apache, then gzip compresses the datestamped log files. As a bonus, it chgrps the compressed logs to whatever group you have set in $gid, and sets permissions up for that group to be able to read or write to them. This makes it simple for other (non-root) processes to come along later and post-process the compressed logs.

Run this nightly (or weekly, depending on your traffic) in cron to collect a regular repository of compressed logs, and keep your live log files down to a manageable size.

Listing: logflume.pl

#!/usr/bin/perl -w # # logflume.pl # # Roll over and compress Apache log files, following Includes within the # httpd.conf (and all other configuration files). # # use strict; $|++; my $server_root = "/usr/local/apache"; my $conf = "$server_root/conf/httpd.conf"; my $gid = "wwwadmin"; my (%logs, %included, @files, @gzip); my $date = `date +%Y%m%d`; chomp $date; push @files, $conf; for $conf (@files) { open(CONF, "<$conf") || die "Cannot open config file $conf: $!\n"; while (<CONF>) { chomp; next if /^(\s+)?#/; if (/(Transfer|Custom|Error)Log\s+(\S+)(\s+)/i) { ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required