2332323 发表于 2017-8-22 08:40:59

nginx日志轮询切割实战

apache切割工具: cronlog 和rotare等日志切割工具
nginx切割工具有: nginx cron+x    scripts

1
2
3
4
5
6
# mkdir -p /server/scripts
# cd /server/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
# /bin/sh /server/scripts/cut_nginx_log.sh
# ls /application/nginx/logs/
access.logerror.lognginx.pidaccess_2017-08-21.log
# date -s "2016/05/14" #设定现在时间为20160514
Sat May 14 00:00:00 CST 2016
# /bin/sh /server/scripts/cut_nginx_log.sh
# ls /application/nginx/logs/            
access_2016-05-14.logaccess.lognginx.pid
access_2017-08-21.logerror.log   www_access_2017-08-21.log




继续优化切割脚本

1
2
3
4
5
6
7
8
# 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
# crontab -e
# 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天意思
页: [1]
查看完整版本: nginx日志轮询切割实战