使用logrotate,对catalina.out日志切片
什么是logrotate
logrotate 程序是一个日志文件管理工具。用于分割日志文件,删除旧的日志文件,并创建新的日志文件,起到“转储”作用。可以节省磁盘空间。
logrotate文件位置
该配置文件位于,/etc/logrotate.conf
,/etc/logrotate.d/
。
[root@localhost etc]# cd /etc/
[root@localhost etc]# ls
adjtime cron.monthly favicon.png hosts.allow logrotate.conf opt rc2.d sestatus.conf system-release
aliases crontab filesystems hosts.deny logrotate.d os-release rc3.d shadow system-release-cpe
aliases.db cron.weekly firewalld init.d lvm pam.d rc4.d shadow- terminfo
alternatives crypttab fonts inittab machine-id passwd rc5.d shells tmpfiles.d
anacrontab csh.cshrc fstab inputrc magic passwd- rc6.d skel tuned
asound.conf csh.login fuse.conf iproute2 mailcap pkcs11 rc.d ssh udev
audisp dbus-1 gcrypt issue makedumpfile.conf.sample pki rc.local ssl vconsole.conf
audit default glvnd issue.net man_db.conf plymouth redhat-release statetab vimrc
bash_completion.d depmod.d gnupg kdump.conf mime.types pm resolv.conf statetab.d virc
bashrc dhcp GREP_COLORS kernel mke2fs.conf polkit-1 resolv.conf.save subgid vmware-tools
binfmt.d DIR_COLORS groff krb5.conf modprobe.d popt.d rpc subgid- wpa_supplicant
centos-release DIR_COLORS.256color group krb5.conf.d modules-load.d postfix rpm subuid X11
centos-release-upstream DIR_COLORS.lightbgcolor group- ld.so.cache motd ppp rsyslog.conf subuid- xdg
chkconfig.d docker grub2.cfg ld.so.conf mtab prelink.conf.d rsyslog.d sudo.conf xinetd.d
chrony.conf dracut.conf grub.d ld.so.conf.d my.cnf printcap rwtab sudoers yum
chrony.keys dracut.conf.d gshadow libaudit.conf my.cnf.d profile rwtab.d sudoers.d yum.conf
containerd e2fsck.conf gshadow- libnl NetworkManager profile.d sasl2 sudo-ldap.conf yum.repos.d
cron.d egl gss libuser.conf networks protocols securetty sysconfig
cron.daily environment host.conf locale.conf nsswitch.conf python security sysctl.conf
cron.deny ethertypes hostname localtime nsswitch.conf.bak rc0.d selinux sysctl.d
cron.hourly exports hosts login.defs openldap rc1.d services systemd
配置logrotate
常见配置参数小结
[root@localhost etc]# vi /etc/logrotate.d/tomcat
#配置你的 catalina.out 日志路径
/home/grkj/tomcat9.0/logs/catalina.out
/home/grkj/tomcat9.0/logs/localhost_access_log.*.txt{
#指定转出周期为每天
daily
#指定⽂件删除前的转储次数,0为每月备份,10为保留10个备份 例如:每天备份一个,备份到10个,第11天删除第一个备份文件
rotate 10
#如果日志不存在则忽略该警告信息
missingok
#⽂件后缀是日期格式
dateext
#启用gzip压缩
compress
#忽略空⽂件
notifempty
#把当前⽂件备份并截短
copytruncate
}
#测试执行 查看配置效果 如下截图
[root@localhost etc]# logrotate --force /etc/logrotate.d/tomcat
配置linux自带系统定时器
[root@localhost etc]# crontab -e
#脚本写到定时计划中去(每天23点59分运⾏)
59 23 * * * /usr/sbin/logrotate -f /etc/logrotate.d/tomcat
重新载入定时配置,启动定时器,查看定时状态
#重启载⼊配置
[root@localhost etc]# systemctl reload crond
#如未启动,则需要启动crontab
[root@localhost etc]# systemctl start cr
[root@localhost etc]# systemctl status crond
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since 二 2021-04-06 16:13:43 CST; 4 months 24 days ago
Process: 1533 ExecReload=/bin/kill -HUP $MAINPID (code=exited, status=0/SUCCESS)
Main PID: 892 (crond)
Tasks: 1
Memory: 1.0M
CGroup: /system.slice/crond.service
└─892 /usr/sbin/crond -n
[root@localhost etc]# systemctl list-unit-files
UNIT FILE STATE
proc-sys-fs-binfmt_misc.automount static
dev-hugepages.mount static
dev-mqueue.mount static
proc-sys-fs-binfmt_misc.mount static
run-vmblock\x2dfuse.mount disabled
sys-fs-fuse-connections.mount static
sys-kernel-config.mount static
sys-kernel-debug.mount static
tmp.mount disabled
brandbot.path disabled
systemd-ask-password-console.path static
systemd-ask-password-plymouth.path static
systemd-ask-password-wall.path static
session-1029.scope static
session-1049.scope static
session-2783.scope static
session-2784.scope static
session-3581.scope static
arp-ethers.service disabled
auditd.service enabled
autovt@.service enabled
blk-availability.service disabled
brandbot.service static
chrony-dnssrv@.service static
chrony-wait.service disabled
#已启动
chronyd.service enabled
console-getty.service disabled
console-shell.service disabled
container-getty@.service static
........