Trace: logrotate

logrotate

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
  • The main logrotate configuration file is here: /etc/logrotate.conf
/app/www/public/data/pages/home_server/home_server_setup/other_services/logrotate.txt · Last modified: 2023-12-26 Tue wk52 11:30
CC Attribution-Share Alike 4.0 International Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International