#925926 /etc/cron.daily/tomcat9: compresses “live” logfiles

#925926#5
Date:
2019-03-05 14:20:36 UTC
From:
To:
/etc/cron.daily/tomcat7 compresses logfiles that are still
“live” and just happen to not have been written to for a
day. The next write to the still-opened logfile will go to
nirvana, and furthermore, the second run fails usually as
a tomcat restart will recreate the uncompressed logfile,
and compressing that fails with the cause that the file
with the .gz extension already exists.

#925926#22
Date:
2021-08-25 06:05:30 UTC
From:
To:
I made a workaround for this, it's extremely gross but does help.

#!/bin/sh
# refresh the mtime of logs that are held open
# run this before /etc/cron.daily/tomcat9
NAME=tomcat9
DEFAULT=/etc/default/$NAME
LOGEXT="log txt"
# Overwrite settings from default file
if [ -f "$DEFAULT" ]; then
        . "$DEFAULT"
fi

TMPDIR=$(mktemp /var/tmp/XXXXXX)
trap '[ -d "$TMPDIR" ] && rm -rf "$TMPDIR"' 0

if [ -d /var/log/$NAME ]; then
        for EXT in $LOGEXT; do
                # Compress the log files
                if [ $LOGFILE_COMPRESS = 1 ]; then
                        loglist="${TMPDIR}/${EXT}"
                        find /var/log/$NAME/ -name \*.$EXT \
                            -daystart -mtime +0 > "$loglist"
                        while read f
                        do
                           if lsof -f -- "$f" 1>/dev/null 2>&1; then
                               touch -m "$f"
                           fi
                        done < "$loglist"
                        rm "$loglist"
                        EXT=$EXT.gz
                fi
        done
fi