Table of Contents

Prev  
 Next
, , , , , ,

logrotate

There are a number of ways to rotate logs.

  1. Using a script and cron (or systemd.timer)
  2. Using logrotate

Logrotate using script

edit the sudo crontab sudo crontab -e:

Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat,sun
# |  |  |  |  |
# *  *  *  *  *   user-name command to be executed
# 0  0  1  *  * /home/shared/Myscripts/rotatelog.sh /var/log/UPS/UPSLog.Log #Rotate NUT UPS log file

where /home/shared/Myscripts/rotatelog.sh is:

#!/bin/bash
logfile=$1
if [ ! -f $logfile ]; then
  echo "log file not found $logfile"
  exit 1
fi
timestamp=`date +%Y%m%d`
newlogfile=$logfile.$timestamp
cp $logfile $newlogfile
cat /dev/null > $logfile
gzip -f -9 $newlogfile

Remember to make executable (sudo chmod +x /home/shared/Myscripts/rotatelog.sh)

logrotate

  1. Install logrotate
    1. debian: sudo apt install logrotate
    2. Arch: sudo pacman -S logrotate

How to Rotate Logs With Logrotate in Linux

Prev  
 Next