logrotate程序是一个日志文件管理工具。用于分割日志文件,删除旧的日志文件,并建立新的日志文件,起到“转储”做用。能够节省磁盘空间。nginx
logrotate命令格式:
logrotate [OPTION...] <configfile>
-d, --debug :debug模式,测试配置文件是否有错误。
-f, --force :强制转储文件。
-m, --mail=command :发送日志到指定邮箱。
-s, --state=statefile :使用指定的状态文件。
-v, --verbose :显示转储过程。
logrotate的配置文件是/etc/logrotate.conf。查看缺省配置状况:vim
cat /etc/logrotate.confide
显示以下:工具
# see "man logrotate" for details
# rotate log files weekly
weeklypost# keep 4 weeks worth of backlogs
rotate 4测试# create new (empty) log files after rotating old ones
createthis# uncomment this if you want your log files compressed
#compressspa# RPM packages drop log rotation information into this directory
include /etc/logrotate.ddebug# no packages own wtmp -- we'll rotate them here
/var/log/wtmp {
monthly
minsize 1M
create 0664 root utmp
rotate 1
}日志# system-specific logs may be also be configured here.
简单说明:
weekly :全部的日志文件每周转储一次。
rotate 4 :转储的文件分为4份。
create :logrotate自动建立新的日志文件。
compress :压缩日志文件。默认是注释掉的。
include /etc/logrotate.d :读入/etc/logrotate.d目录下的日志转储参数,当系统中安装了RPM软件包时,RPM包的日志转储参数通常会自动创建在/etc/logrotate.d目录下。
/var/log/wtmp段 :对/var/log/wtmp日志转储的配置。
使用logrotate管理lnmp一键安装包中nginx的链接日志,lnmp日志文件在/home/wwwlogs目录下。
创建配置文件:
vim /etc/logrotate.d/nginx
输入以下:
/home/wwwlogs/access.log /home/wwwlogs/nginx_error.log {
notifempty
daily
rotate 5
sharedscripts
postrotate
/bin/kill -HUP `/bin/cat /usr/local/nginx/logs/nginx.pid`
endscript
}
说明:
notifempty :若是是空文件的话,不转储。
daily :日志文件天天转储一次。
rotate 5 ;转储文件分为5份。
postrotate/endscript :日志转储后执行的脚本。这里用来让nginx从新生成日志文件。nginx.pid里存的是nginx的主进程号。
执行logrotate:
/usr/sbin/logrotate -vf /etc/logrotate.conf
若是没有报错,生成了转储文件,nginx正常访问,就OK了。
logrotate如何自动执行:在/etc/cron.daily目录下有logrotate执行的脚本。经过crontab程序天天执行一次。