frantisek holop wrote:
Come on man .. 'can of worms' ?! It's not even a real challenge.
you left off the year...

i'd rather not bloat my 3 line scripts with lookup tables
and recursive date(1) calls, i prefer them easy and short.

here's the full picture of the current situation:

on 1. april i get this file through monthly.local:

logfile-2010-04.log.gz

containing all _march_ entries (plus some april if it creeped in).

i have 2 choices:
1. do the rename dance backwards one month
2. let the job actually run sometimes before midnight, last day of the month.

Not that it helps "unbloating" your scripts, but when I have that problem I usually cheat by running my script twice - once a few minutes before midnight (as cron doesn't guarantee execution at a specific time, only "at or after", we need a margin) and once after.

The first time the script is called (with a switch set to indicate it's the pre-run) it creates a file containing the current date in a suitable format. The next time, it uses that file for the rotated log file name.

Here's an example I use to rotate my apache log files. First the crontab entries:

----8<--------8<--------8<--------8<--------8<--------8<---- (cut)
50      23      *       *       *       /var/www/logs/archivelogs -d
1       0       *       *       *       /var/www/logs/archivelogs
----8<--------8<--------8<--------8<--------8<--------8<---- (cut)

Then the script itself:

----8<--------8<--------8<--------8<--------8<--------8<---- (cut)
#!/bin/sh

cd /var/www/logs

if [ ! -f "./archivedate" ]
then
        if [ "$1" = "-d" ]
        then
            date '+%y%m%d' >./archivedate
            exit 0
        fi

        echo "$0: error: no /var/www/logs/archivedate"
        exit 1
fi

ARCHDATE=`cat ./archivedate`

# Make archive directory if it doesn't exist already
mkdir -p ./Archive

if [ -f "./Archive/access-$ARCHDATE.log" -o -f \
        "./Archive/access-$ARCHDATE.log.gz" ]
then
echo "$0: error: access.log archive file for $ARCHDATE already there"
        exit 2
fi

if [ -f "./Archive/error-$ARCHDATE.log" -o -f \
        "./Archive/error-$ARCHDATE.log.gz" ]
then
echo "$0: error: error.log archive file for $ARCHDATE already there"
        exit 3
fi

mv ./access.log ./Archive/access-$ARCHDATE.log
mv ./error.log ./Archive/error-$ARCHDATE.log

# Hopefully there is a better way to do this...
#/usr/sbin/apachectl stop
#sleep 5
#/usr/bin/nice -8 /usr/sbin/httpd -u
apachectl graceful

sleep 180

gzip -q ./Archive/access-$ARCHDATE.log
gzip -q ./Archive/error-$ARCHDATE.log

rm -f "./archivedate"

exit 0
----8<--------8<--------8<--------8<--------8<--------8<---- (cut)


Best regards,

/Benny

--
internetlabbet.se     / work:   +46 8 551 124 80      / "Words must
Benny LC6fgren        /  mobile: +46 70 718 11 90     /   be weighed,
                    /   fax:    +46 8 551 124 89    /    not counted."
                   /    email:  benny aa internetlabbet.se

Reply via email to