系统环境: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 =
# user= root
#
# where "
#
# 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
有需求如下,每天晚上零点切割tomcat的日志,每个月30号使用脚本将上个月的转储日志文件压缩并移走
每天的作业:由root执行
cat /etc/logrotate.d/tomcatday
/usr/local/apache-tomcat-7.0.90/logs/*.out {
copytruncate #用于还在打开中的日志文件,把当前日志备份并截断
#daily #计划每天执行,如果放在/etc/crontab的计划任务里面,可以省略这个参数
rotate 365 #保留的个数
dateext #使用当期日期作为命名格式
dateformat .%Y-%m-%d #指定生成文件的日期格式
create 0644 root utmp #权限
}
每个月执行的脚本:
#cat /shell/mvtomcatlog.sh
#!/bin/bash
a=`date +%Y-%m-%d -d -1month` #取值上个月的今天
b="catalina.out."$a #取值如catalina.out.2018-10-12,这将是日志目录转储以后的格式
c=${b::-2} #去掉最后两个字符,通配上个月的全部转储日志文件,CENTOS6用这个c=${b::$((${#b}- 2))}
cd /usr/local/apache-tomcat-7.0.90/logs/
ls /usr/local/apache-tomcat-7.0.90/logs/ | grep $c | xargs -I {} gzip {} #用gzip压缩全部上个月的转储日志文件,默认源文件不保存
ls | grep gz | xargs -I {} mv {} ../oldlogs/ #将压缩好的文件移到另一个目录
ls /usr/local/apache-tomcat-7.0.90/logs/ | grep $c | xargs -I {} mv {} /usr/local/apache-tomcat-7.0.90/oldlogs/ #将一个月以前的日志移到每月任务处理的目录
然后crontab -e编辑每天和每月任务执行的时间,如:30 17 * * * /usr/sbin/logrotate -f /etc/logrotate.d/tomcatday
在cron当中,命令的路径要打全/usr/sbin/logrotate,不能直接logrotate这样。
另外,系统自动每天每周每月配置的任务执行的时间可以根据这些文件的时间判断:
[root@V71 rundeck]# ll /var/spool/anacron/
总用量 12
-rw-------. 1 root root 9 10月 18 09:22 cron.daily
-rw------- 1 root root 9 10月 13 18:28 cron.monthly
-rw-------. 1 root root 9 10月 14 15:29 cron.weekly