系统环境:CENTOS7
系统默认每天会执行:/usr/sbin/logrotate /etc/logrotate.conf
[root@V71 logs]# cat /etc/cron.daily/logrotate
#!/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
[root@V71 logs]#
默认执行的时间
[root@V71 logs]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# 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
# | | | | |
# * * * * * user-name command to be executed
01 * * * * root run-parts /etc/cron.hourly
30 21 * * * root run-parts /etc/cron.daily #每天的计划任务执行的时间,可根据实际情况更改
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
配置日志切割程序每天处理哪些目录,哪些文件,可以在两个地方配置
文件:/etc/logrotate.conf
目录:/etc/logrotate.d/
[root@V71 logs]# cat /etc/logrotate.conf | grep -v ^#
weekly
rotate 4
create
dateext
include /etc/logrotate.d #include 了这个目录
/var/log/wtmp {
monthly
create 0664 root utmp
minsize 1M
rotate 1
}
/usr/local/apache-tomcat-7.0.90/logs/*.log {
size 1k
compress
create 0700 root root
rotate 3
olddir /usr/local/apache-tomcat-7.0.90/oldlogs/
}
/usr/local/apache-tomcat-7.0.90/logs/*.out {
size 1k
compress
create 0700 root root
rotate 3
olddir /usr/local/apache-tomcat-7.0.90/oldlogs/
}
/usr/local/apache-tomcat-7.0.90/logs/*.txt {
size 1k
compress
create 0700 root root
rotate 3
olddir /usr/local/apache-tomcat-7.0.90/oldlogs/
}
将准备分割的日志定在 /etc/logrotate.d目录下的文件里面也是一样的,例如:
[root@V71 logs]# cat /etc/logrotate.d/httpd
/var/log/httpd/*log {
missingok
notifempty
sharedscripts
delaycompress
postrotate
/bin/systemctl reload httpd.service > /dev/null 2>/dev/null || true
endscript
}
[root@V71 logs]# cat /etc/logrotate.d/mysql
# This logname can be set in /etc/my.cnf
# by setting the variable "err-log"
# in the [safe_mysqld] section as follows:
#
# [safe_mysqld]
# err-log=/var/lib/mysql/mysqld.log
#
# If the root user has a password you have to create a
# /root/.my.cnf configuration file with the following
# content:
#
# [mysqladmin]
# password = <secret>
# user= root
#
# where "<secret>" is the password.
#
# ATTENTION: This /root/.my.cnf should be readable ONLY
# for root !
/var/lib/mysql/mysqld.log {
# create 600 mysql mysql
notifempty
daily
rotate 3
missingok
compress
postrotate
# just if mysqld is really running
if test -x /usr/bin/mysqladmin && \
/usr/bin/mysqladmin ping &>/dev/null
then
/usr/bin/mysqladmin flush-logs
fi
endscript
}
立即启动日志切割:/usr/sbin/logrotate -f /etc/logrotate.conf