博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
nginx日志轮询切割实战
阅读量:6887 次
发布时间:2019-06-27

本文共 1574 字,大约阅读时间需要 5 分钟。

apache切割工具: cronlog 和rotare等日志切割工具

nginx切割工具有: nginx cron+x    scripts

1
2
3
4
5
6
[root@web01 ~]
# mkdir -p /server/scripts
[root@web01 ~]
# cd /server/scripts/
[root@web01 scripts]
# vim cut_nginx_log.sh #vim输入如下内容
cd 
/application/nginx/logs
/bin/mv 
access.log access_$(
date 
+%F).log
/application/nginx/sbin/nginx 
-s reload

 

1
2
3
4
5
6
7
8
9
[root@web01 logs]
# /bin/sh /server/scripts/cut_nginx_log.sh
[root@web01 scripts]
# ls /application/nginx/logs/
access.log  error.log  nginx.pid  access_2017-08-21.log
[root@web01 scripts]
# date -s "2016/05/14" #设定现在时间为20160514
Sat May 14 00:00:00 CST 2016
[root@web01 scripts]
# /bin/sh /server/scripts/cut_nginx_log.sh 
[root@web01 scripts]
# ls /application/nginx/logs/              
access_2016-05-14.log  access.log  nginx.pid
access_2017-08-21.log  error.log   www_access_2017-08-21.log

继续优化切割脚本

1
2
3
4
5
6
7
8
[root@web01 scripts]
# vim cut_nginx_log.sh 
cd 
/application/nginx/logs
/bin/mv 
access.log access_$(
date 
+%F).log
/application/nginx/sbin/nginx 
-s reload
#
rsync 
.....to backup server
#del date before 7 day
find 
...

设置定时任务:每天00:00切换日志

1
2
3
4
5
6
7
8
[root@web01 scripts]
# crontab -e
[root@web01 scripts]
# crontab -l
#time sync by oldboy at 2010-2-1
*
/5 
* * * * 
/usr/sbin/ntpdate 
time
.nist.gov >
/dev/null 
2>&1
#backup
00 00 * * * 
/bin/bash 
/server/scripts/backup
.sh &>
/dev/null
######cut_nginx_log#####
00 00 * * * 
/bin/sh 
/server/scripts/cut_nginx_log
.sh>
/dev/null 
2>&1   
##这个就是切割日志

上述脚本时间可以写成access_$(date %F -d '-1day').log ##就是当前日志-1天意思

本文转自sandshell博客51CTO博客,原文链接http://blog.51cto.com/sandshell/1958181如需转载请自行联系原作者

sandshell

你可能感兴趣的文章