Log in

No account? Create an account

Previous Entry | Next Entry

Using inotifywait to compress 389 log files

inotifywait is used to efficiently poll for changes to a file or directory. On Red Hat/Fedora family operating systems, this is provided by the inotify-tools package - yum install inotify-tools. inotifywait can be used to watch for rotated access log files to compress. For example, if your access logs grow large due to a lot of traffic, you may want to compress the files to save disk space. In my testing, I have found that compressing an access log file using bzip2 reduces the size to about 3% of the original size. When the directory server rotates a log file, it issues a rename(), then recreates the access log file "access" again. You can use a shell script like this to monitor and compress rotated access log files:
# monitor and compress access logs
mon_and_comp_logs() {
    # the server does rename access -> access.20..... when rotating, then
    # creates a new file named "access"
    inotifywait -e move /var/log/dirsrv/slapd-INST --exclude '.bz2$' -m | while read dir act name ; do
        case "$file" in
            echo compressing $file
            bzip2 $file &

Then run this in the background:
# mon_and_comp_logs > mon_and_comp_logs.log 2>&1 &