Log rotation is especially useful for log files with fixed file names, such as MySQL error log, regular query log, slow query log, and so on. Linux system has a very easy to use according to Logratate can achieve automatic rotation, this article describes its principle and usage.
By default, Logrotate is deployed as a daily cron job, and you can find a configuration file named Logrotate in the directory/etc/cron.daily. So it runs at the top of the day? Open the file/etc/crontab will know, the following is my machine on the situation:
Shell=/bin/bash
path=/sbin:/bin:/usr/sbin:/usr/bin
mailto=root
home=/
# run-parts
01 * * * * * * Root run-parts/etc/cron.hourly
4 * * * * Root run-parts/etc/cron.daily
4 * * 0 root Run-parts/etc/cron.week LY
4 1 * * Root run-parts/etc/cron.monthly
From the above configuration we can know that the/etc/cron.daily is executed at 4:02 every day. That is, every day 4:02/etc/cron.daily/logrotate will be automatically executed, the following is its content:
#!/bin/sh
/usr/sbin/logrotate/etc/logrotate.conf
exitvalue=$?
If [$EXITVALUE!= 0]; Then
/usr/bin/logger-t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0
From the above we can see that the default profile for Logratate is/etc/logratate.conf, and here's what it does:
Exitvalue=$?
If [$EXITVALUE!= 0]; Then
/usr/bin/logger-t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0
[root@lx202/ etc/cron.daily]# cat/etc/logrotate.conf
# "man logrotate" for details
# rotate log files weekly
9/># Keep 4 weeks worth of backlogs
rotate 4
# Create new (empty) log files after rotating old ones
create< c13/># Uncomment this if you want your log files compressed
#compress
# RPM Packages drop log Rotation informati On to this directory
INCLUDE/ETC/LOGROTATE.D
# no packages own wtmp--we'll rotate them here
/var/log/w TMP {
monthly
minsize 1M
Create 0664 root utmp
rotate 1
}
/var/log/btmp {
Missingok
monthly
minsize 1M
Create 0600 root utmp
rotate 1
}