logrotate管理nginx日志文件
轉(zhuǎn)自:http://linux008.blog.51cto.com/2837805/555829/
描述:linux日志文件如果不定期清理,會(huì)填滿整個(gè)磁盤。這樣會(huì)很危險(xiǎn),因此日志管理是系統(tǒng)管理員日常工作之一。我們可以使用"logrotate"來管理linux日志文件,它可以實(shí)現(xiàn)日志的自動(dòng)滾動(dòng),日志歸檔等功能。下面以nginx日志文件來講解下logrotate的用法。
配置:
1、在/etc/logrotate.d目錄下創(chuàng)建一個(gè)nginx的配置文件"nginx"配置內(nèi)容如下
#vim /etc/logrotate.d/nginx
/usr/local/nginx/logs/*.log {
daily
rotate 5
missingok
notifempty
sharedscripts
postrotate
? ? if [ -f /usr/local/nginx/logs/nginx.pid ]; then
? ? ? ? kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
? ? fi
endscript
}
mongodb截取日志
# mongodb-log rotate
/data/var/log/mongodb/mongod.log {
? ? daily
dateext
? ? rotate 30
? ? copytruncate
? ? missingok
}
logrotate -vf /etc/logrotate.d/mongodb-log
保存退出。
2、執(zhí)行l(wèi)ogrotate
#/usr/sbin/logrotate -f /etc/logrotate.d/nginx
在/usr/local/nginx/logs目錄中會(huì)產(chǎn)生
error.log
error.log.1
說明logrotate配置成功。
3、讓logrotate每天進(jìn)行一次滾動(dòng),在crontab中添加一行定時(shí)腳本。
#crontab -e
59 23 * * * ?/usr/sbin/logrotate -f /etc/logrotate.d/nginx
每天23點(diǎn)59分進(jìn)行日志滾動(dòng)
4、配置文件說明
dateext:以日期為后綴
daily:日志文件每天進(jìn)行滾動(dòng)
rotate:保留最5次滾動(dòng)的日志
notifempty:日志文件為空不進(jìn)行滾動(dòng)
copytruncate:保留種子日志
sharedscripts:運(yùn)行postrotate腳本
下面是一個(gè)腳本
postrotate
? ? if [ -f /usr/local/nginx/logs/nginx.pid ]; then
? ? ? ? kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
? ? fi
endscript
腳本讓nginx重新生成日志文件。
轉(zhuǎn)載于:https://blog.51cto.com/winchoo/1619406
總結(jié)
以上是生活随笔為你收集整理的logrotate管理nginx日志文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 简易的AJAX工具[转]
- 下一篇: 歪枣网数据库设计-千万级别海量数据查询效