您的位置:首页 > 运维架构 > Nginx

nginx日志轮询切割实战

2017-08-21 21:43 375 查看
apache切割工具: cronlog 和rotare等日志切割工具
nginx切割工具有: nginx cron+x scripts
[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

[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
继续优化切割脚本
[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切换日志
[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天意思
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux