Rotating Log Files
There are many reasons why you would want your own script to rotate log files. For instance, if you initially installed Linux to your hard drive via a Knoppix live CD, and then upgraded (or in many cases, downgraded) to a Debian installation, the rotation of log files may not occur properly. If the log files are not being rotated, they could eventually fill up the partition that contains /var (this is bad).In my case, I wanted the log files to only be rotated once every other month, and for only the last three archived copies of the log files to be kept around. The way that I chose to implement this was in a shell script, and the script is run by a cron job. The shell script was called "/root/rotatelogs", was set to be executable by root, and only readable by everyone else.
(written 1/6/2006)
How the Script Works
The script is kind of long, which is why the contents are listed in the last section.To explain what the script does, let's look at the auth.log file. The previous 2 month span would be archived in auth.log.old.1. This file is followed by auth.log.old.2 with contains the logs from the previous 2 months, etc. auth.log.old.3 contains the oldest logs.
The first thing the script does is delete the oldest archived log file (auth.log.old.3). It then increments the names of the other archives such that the contents of auth.log.old.2 becomes the oldest archived log file. After all of the archived log files are incremented, it takes the contents of the currently open auth.log file, and archives it by moving it to auth.log.old.1.
This procedure is then performed on the daemon.log, kern.log, messages, syslog, and wtmp files. Finally, at the end of the script, syslogd is restarted, which causes it to start writing to the brand new, empty log files.
I realize that the script is kind of a hack, but I'm not that great with shell scripts, and this was the only way I could figure out how to accomplish the task. And it works.
Additional disk space could be saved by gzip'ing each new log file after it is created.
(written 1/6/2006)
Adding the Script to the Crontab
To add the script to the crontab, first create a text file that defines how often the script will be run. For the rotatelogs script, I created /root/rotatelogs-crontab.txt. This file only contains one line:0 0 1 2,4,6,8,10,12 * /root/rotatelogs
The first two zeros define the minute and hour of the day that the script should be run (0:00=midnight). The 1 defines the day of the month that the script should be run. The 2,4,6,8,10,12 define the months that the script should run (Feb, Apr, Jun, Aug, Oct, Dec). Finally, the * defines the day of the week that the script should run (* = any day of the week). Therefore, this crontab should cause the script to execute at midnight, on the first day of every other month, no matter what day that falls on.
Let's say you wanted the script instead to run at midnight, on the first day of every month, no matter what day it falls on. Then the crontab would contain the following line:
0 0 1 * * /root/rotatelogs
Finally, this file needs to be uploaded to the crontab spool area.
crontab /root/rotatelogs-crontab.txt
You can list the contents of the crontab spool area to verify that it was uploaded properly:
crontab -l
To delete the crontab, use the following:
crontab -r
(written 1/6/2006)
rotatelogs file
The contents of the shell script are as follows:#!/bin/sh -
# This is an attempt to write a script to manually
# rotate the log files. It will then be uploaded to the
# root's crontab using the /root/rotatelogs-crontab.txt
# file.
#
# This script is supposed to rotate the /var/log/auth.log,
# /var/log/daemon.log, /var/log/kern.log, /var/log/messages,
# /var/log/syslog, and /var/log/wtmp files. It will save 3
# of the newest rotated log files for each of those logs,
# and is designed to be exected once every other month
# (0 0 1 2,4,6,8,10,12 * /root/rotatelogs).
if [ -f /var/log/auth.log.old.3 ] ; then
rm /var/log/auth.log.old.3
fi
if [ -f /var/log/auth.log.old.2 ] ; then
mv /var/log/auth.log.old.2 /var/log/auth.log.old.3
fi
if [ -f /var/log/auth.log.old.1 ] ; then
mv /var/log/auth.log.old.1 /var/log/auth.log.old.2
fi
if [ -f /var/log/auth.log ] ; then
mv /var/log/auth.log /var/log/auth.log.old.1
touch /var/log/auth.log
fi
if [ -f /var/log/daemon.log.old.3 ] ; then
rm /var/log/daemon.log.old.3
fi
if [ -f /var/log/daemon.log.old.2 ] ; then
mv /var/log/daemon.log.old.2 /var/log/daemon.log.old.3
fi
if [ -f /var/log/daemon.log.old.1 ] ; then
mv /var/log/daemon.log.old.1 /var/log/daemon.log.old.2
fi
if [ -f /var/log/daemon.log ] ; then
mv /var/log/daemon.log /var/log/daemon.log.old.1
touch /var/log/daemon.log
fi
if [ -f /var/log/kern.log.old.3 ] ; then
rm /var/log/kern.log.old.3
fi
if [ -f /var/log/kern.log.old.2 ] ; then
mv /var/log/kern.log.old.2 /var/log/kern.log.old.3
fi
if [ -f /var/log/kern.log.old.1 ] ; then
mv /var/log/kern.log.old.1 /var/log/kern.log.old.2
fi
if [ -f /var/log/kern.log ] ; then
mv /var/log/kern.log /var/log/kern.log.old.1
touch /var/log/kern.log
fi
if [ -f /var/log/messages.old.3 ] ; then
rm /var/log/messages.old.3
fi
if [ -f /var/log/messages.old.2 ] ; then
mv /var/log/messages.old.2 /var/log/messages.old.3
fi
if [ -f /var/log/messages.old.1 ] ; then
mv /var/log/messages.old.1 /var/log/messages.old.2
fi
if [ -f /var/log/messages ] ; then
mv /var/log/messages /var/log/messages.old.1
touch /var/log/messages
fi
if [ -f /var/log/syslog.old.3 ] ; then
rm /var/log/syslog.old.3
fi
if [ -f /var/log/syslog.old.2 ] ; then
mv /var/log/syslog.old.2 /var/log/syslog.old.3
fi
if [ -f /var/log/syslog.old.1 ] ; then
mv /var/log/syslog.old.1 /var/log/syslog.old.2
fi
if [ -f /var/log/syslog ] ; then
mv /var/log/syslog /var/log/syslog.old.1
touch /var/log/syslog
fi
if [ -f /var/log/wtmp.old.3 ] ; then
rm /var/log/wtmp.old.3
fi
if [ -f /var/log/wtmp.old.2 ] ; then
mv /var/log/wtmp.old.2 /var/log/wtmp.old.3
fi
if [ -f /var/log/wtmp.old.1 ] ; then
mv /var/log/wtmp.old.1 /var/log/wtmp.old.2
fi
if [ -f /var/log/wtmp ] ; then
mv /var/log/wtmp /var/log/wtmp.old.1
touch /var/log/wtmp
fi
# restart syslogd
/etc/init.d/sysklogd reload-or-restart > /dev/null
(written 1/6/2006)
