Problem
How do I automatically rotate my system log files (syslog messages xferlog daemonlog)?
Solution
The following script could be used for this purpose:
Steps:1. Create a script in /usr/local/bin/rotatesyslogs with the following:
-----------
#! /bin/sh
#
# rotatesyslogs 1.0.0 Rotates System logs...
#
# Created by Boyd Gerber
# Created November 1994
#
# Modified March 2019
##
# Set up variable and Build up the list of log files to process.
#
PATH=:/usr/local/bin:/usr/bin:/bin:/etc:
export PATH#
# Set defaults.
#LOGDIR="/usr/adm/"
LOG="syslog messages xferlog daemonlog"
LOGS="${LOG}"
OLD="OLD"#
# Make sure $OLD exists
#if [ ! -d $LOGDIR/$OLD ]; then
mkdir -p $LOGDIR/$OLD
fi#
# COMPRESS is the type of compression to use (compress, gzip, ...)
#COMPRESS=gzip
Z=".gz"#
# Set number of cycles i.e 31 days if run once a day.
#CYCLES=31for F in ${LOGS} ; do#
# Process the curren log
#BASE=`basename ${F}`
if [ -f ${LOGDIR}/${OLD}/${BASE} ]; then
rm -f ${LOGDIR}/${OLD}/${BASE}
fi#
# Make sure we have the log file, a .old log file
# if [ -f ${LOGDIR}/${F} ]; then
cp ${LOGDIR}/${F} ${LOGDIR}/${F}.old
cat /dev/null > ${LOGDIR}/${F}
mv ${LOGDIR}/${F}.old ${LOGDIR}/${OLD}/${BASE}
fi
done#
# We now (finally!) have copies of the log files where we need them.
#
# Compress and rotate the logs.
#for F in ${LOGS} ; do#
# Skip if file doesn't exist.
#BASE=`basename ${F}`
test -f ${LOGDIR}/${OLD}/${BASE} || continue#
# Compress the file.
# ${COMPRESS} < ${LOGDIR}/${OLD}/${BASE} > ${LOGDIR}/${OLD}/${BASE}.0${Z} \
&& rm -f ${LOGDIR}/${OLD}/${BASE}
chmod 0444 ${LOGDIR}/${OLD}/${BASE}.0${Z}#
# Do rotation.
# EXT=${CYCLES}
rm -f ${LOGDIR}/${OLD}/${BASE}.${CYCLES}${Z}
while [ ${EXT} -gt 0 ] ; do
NEXT=${EXT}
EXT=`expr ${EXT} - 1`
test -f ${LOGDIR}/${OLD}/${BASE}.${EXT}${Z} \
&& rm -f ${LOGDIR}/${OLD}/${BASE}.${NEXT}${Z} \
&& mv ${LOGDIR}/${OLD}/${BASE}.${EXT}${Z} \
${LOGDIR}/${OLD}/${BASE}.${NEXT}${Z}
done
done# All done.exit 0------------2. Create a entry in /var/spool/cron/crontabs/root. For example as root do the following adding the entry:
# crontab -e
0 0 * * * /usr/local/bin/rotatesyslogsNOTE: Also available at:
ftp://ftpput.sco.com//tmp/support/tools/rotatesyslogs
or
ftp://ftp.zenez.com/pub/zenez/prgms/rotatesyslogs