使用logrotate切割nginx日志文件,其他日志文件切割类似
首先安裝logrotate
以centos為例
yum -y install logrotate
如果不需要切割其他的日志則把
/etc/logrotate.conf中的
/var/log/wtmp {
?? ?monthly
? ? create 0664 root utmp
? ? ? ?minsize 1M
?? ?rotate 1
}
/var/log/btmp {
?? ?missingok
? ? monthly
? ??create 0600 root utmp
? ??rotate 1
}
去掉或者注釋掉
刪除掉/etc/logrotate.d下的文件,然后新建一個(gè)nginx
vi ?/etc/logrotate.d/nginx
內(nèi)容如下
/home/kong/logs/proxy/*.log {
daily
missingok
rotate 90
dateext
#compress
delaycompress
notifempty
create 777 root root
sharedscripts
postrotate
kong restart? ?#我使用的是kong網(wǎng)關(guān),kong網(wǎng)關(guān)重啟使用命令kong restart 如果使用的是普通nginx則可使用(?[ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
? ? ? ? ? ? ? ? ? ? ? ? ? ? //重啟nginx,重新加載日志文件,防止不寫(xiě)
? ? ? ? ? ? `cp -f /var/log/nginx/*.* /var/log/nginx/backup`
? ? ? ? ? ? ? ? ? ? ? ? ? ? //自定義腳本,將舊日志copy到backup文件夾(backup要存在))
endscript
}
其中各個(gè)配置解釋如下:
compress ? ? ? ? ? ? ? ? ? ? ? ?通過(guò)gzip壓縮轉(zhuǎn)儲(chǔ)以后的日志
nocompress ? ? ? ? ? ? ? ? ? ? ?不壓縮
copytruncate ? ? ? ? ? ? ? ? ? ?用于還在打開(kāi)中的日志文件,把當(dāng)前日志備份并截?cái)?br /> nocopytruncate ? ? ? ? ? ? ? ? ?備份日志文件但是不截?cái)?br /> create mode owner group ? ? ? ? 轉(zhuǎn)儲(chǔ)文件,使用指定的文件模式創(chuàng)建新的日志文件
nocreate ? ? ? ? ? ? ? ? ? ? ? ?不建立新的日志文件
delaycompress 和 compress ? ? ? ?一起使用時(shí),轉(zhuǎn)儲(chǔ)的日志文件到下一次轉(zhuǎn)儲(chǔ)時(shí)才壓縮
nodelaycompress ? ? ? ? ? ? ? ? 覆蓋 delaycompress 選項(xiàng),轉(zhuǎn)儲(chǔ)同時(shí)壓縮。
errors address ? ? ? ? ? ? ? ? ? 專(zhuān)儲(chǔ)時(shí)的錯(cuò)誤信息發(fā)送到指定的Email 地址
ifempty ? ? ? ? ? ? ? ? ? ? ? ? 即使是空文件也轉(zhuǎn)儲(chǔ),這個(gè)是 logrotate 的缺省選項(xiàng)。
notifempty ? ? ? ? ? ? ? ? ? ? ?如果是空文件的話,不轉(zhuǎn)儲(chǔ)
mail address ? ? ? ? ? ? ? ? ? ?把轉(zhuǎn)儲(chǔ)的日志文件發(fā)送到指定的E-mail 地址
nomail ? ? ? ? ? ? ? ? ? ? ? ? ?轉(zhuǎn)儲(chǔ)時(shí)不發(fā)送日志文件
olddir directory ? ? ? ? ? ? ? ?轉(zhuǎn)儲(chǔ)后的日志文件放入指定的目錄,必須和當(dāng)前日志文件在同一個(gè)文件系統(tǒng)
noolddir ? ? ? ? ? ? ? ? ? ? ? ?轉(zhuǎn)儲(chǔ)后的日志文件和當(dāng)前日志文件放在同一個(gè)目錄下
prerotate/endscript ? ? ? ? ? ? 在轉(zhuǎn)儲(chǔ)以前需要執(zhí)行的命令可以放入這個(gè)對(duì),這兩個(gè)關(guān)鍵字必須單獨(dú)成行
daily ? ? ? ? ? ? ? ? ? ? ? ? ? 指定轉(zhuǎn)儲(chǔ)周期為每天
weekly ? ? ? ? ? ? ? ? ? ? ? ? ?指定轉(zhuǎn)儲(chǔ)周期為每周
monthly ? ? ? ? ? ? ? ? ? ? ? ? 指定轉(zhuǎn)儲(chǔ)周期為每月
rotate count ? ? ? ? ? ? ? ? ? ?指定日志文件刪除之前轉(zhuǎn)儲(chǔ)的次數(shù),0 指沒(méi)有備份,5 指保留5 個(gè)備份
tabootext [+] list 讓logrotate ? 不轉(zhuǎn)儲(chǔ)指定擴(kuò)展名的文件,缺省的擴(kuò)展名是:.rpm-orig, .rpmsave, v, 和 ~?
size size ? ? ? ? ? ? ? ? ? ? ? 當(dāng)日志文件到達(dá)指定的大小時(shí)才轉(zhuǎn)儲(chǔ),bytes(缺省)及KB(sizek)或MB(sizem)
?
查看logrotate默認(rèn)執(zhí)行時(shí)間
logrotate切割時(shí)間默認(rèn)是在每天的3:22。這個(gè)時(shí)間點(diǎn)可以通過(guò)crontab配置文件查看到。如下:
cat /etc/anacrontab
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22
1 5 cron.daily nice run-parts /etc/cron.daily
7 25 cron.weekly nice run-parts /etc/cron.weekly
@monthly 45 cron.monthly nice run-parts /etc/cron.monthly
其中START_HOURS_RANGE參數(shù)就是配置logrotate切割的時(shí)間點(diǎn)。
執(zhí)行l(wèi)ogrotate -vf /etc/logrotate.d/nginx 測(cè)試
總結(jié)
以上是生活随笔為你收集整理的使用logrotate切割nginx日志文件,其他日志文件切割类似的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: weed mount 之后出现文件删除不
- 下一篇: centos7防火墙操作