logrotate
There are a number of ways to rotate logs.
- Using a script and cron (or systemd.timer)
- 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
- Install logrotate
- debian:
sudo apt install logrotate
- Arch:
sudo pacman -S logrotate
- The main logrotate configuration file is here:
/etc/logrotate.conf