Technical article 127952

How do I automatically rotate my system log files?

OpenServer Definitive 2018UnixWare 7 Definitive 2018OpenServer 6OpenServer 5UnixWare 7

Written · Last revised

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=31
for 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/rotatesyslogs
NOTE:     Also available at:
          ftp://ftpput.sco.com//tmp/support/tools/rotatesyslogs
          or
          ftp://ftp.zenez.com/pub/zenez/prgms/rotatesyslogs

From the Xinuos technical article database, record 127952. Reproduced with permission. The original is at www.sco.com/ta/127952, which is reachable but blocked from search engines. Xinuos marks this article against a retired product line. That refers to the product’s lifecycle, not to whether the advice still works — plenty of it still applies to running systems.

Next step

Need this fixed rather than explained?

Tell us the release, what the system runs and what it is doing. We will reply by email with what we would do next.

  • You deal with the people who do the work
  • No obligation, and no upgrade required before we will help
  • We tell you plainly if something is not worth doing

Send an enquiry

We reply by email. If a server is down, say so in the first line. You can also write to help@scoexpert.com.