最近在看日志管理的资料,发现网上很多朋友做日志分割很多情况是用脚本+cron实现(自己以前也是这样做的),但恰恰忘记logrotate这个好用的工具。它可以自动对日志进行截断(或轮循)、压缩以及删除旧的日志文件。Linux系统/var/log目录下的系统日志就是通过它实现的。
准备
一般主流的CentOS、Debian和Ubuntu系统都自带有安装。确认是否有该命令,执行logrotate。下面以Ubuntu系统和分割Apache日志为例,详细介绍logrotate的配置。
zhangnq@ubuntu:~$ logrotate
logrotate 3.7.8 - Copyright (C) 1995-2001 Red Hat, Inc.
This may be freely redistributed under the terms of the GNU Public License
用法: logrotate [-dfv?] [-d|--debug] [-f|--force] [-m|--mail=command] [-s|--state=statefile] [-v|--verbose] [-?|--help] [--usage] [OPTION...]
说明已经安装,如果提示命令不存在,就执行手动安装。
apt-get install logrotate
简单了解
logrotate是通过cron来执行,触发脚本放置在/etc/cron.daily/logrotate,内容类似如下。运行时logrotate会调用/etc/logrotate.conf主配置文件,每个应用配置放置在/etc/logrotate.d目录。
#!/bin/sh
# Clean non existent log file entries from