项目内log
删除项目内三天前log
创建 /auto_remove.sh 文件:
# find xxx -mtime +3 是指查找*修改时间在三天前的文件*
find /projects/xxx/current/log/* -mtime +3 -delete
crontab 添加:
# 每天零点1分删除三天前log,并将当天rails.log文件清空
1 0 * * * sh /opt/shell/auto_remove_log.sh
1 0 * * * cd /projects/xxx/current/log && echo '' > rails.log
使用logrotate定期拆分处理nginx log
参考:http://siwei.me/blog/posts/rotate-ngin-logs
https://www.cnblogs.com/dadonggg/p/8649706.html
/etc/logrortate.d/nginx :
"/var/log/nginx/access.log" "/var/log/nginx/error.log"{
daily
rotate 7
dateext
copytruncate
missingok
notifempty
delaycompress
sharedscripts
postrotate
# this just simply make nginx start a new log file, but NOT restart. ( quite fast)
test ! -f /run/nginx.pid || kill -USR1 `cat /run/nginx.pid`
endscript
}
crontab 添加上述文件(或者添加/etc/logrotate.conf文件也ok,又或者参考https://www.cnblogs.com/276815076/p/7053640.html
创建logrotate文件放在etc/cron.daily/目录下):
而如果cron.daily内的定时任务同一的是非0点执行(比如6:00),我们缺需要0点就切割nginx日志,完全可以crontab -e 内放如下代码,且不用担心(6:00)重复切割,因为 /var/lib/logrotate/status内记录了0点切割过,不超过1天(设置了daily)就不会重复切割
0 0 * * * logrotate -v /etc/logrotate.d/nginx
网友评论