Created
June 9, 2013 12:06
-
-
Save azhai/5743307 to your computer and use it in GitHub Desktop.
Nginx日志按日期切割
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
cd /opt/nginx-1.4.0/logs/ | |
#分解旧日志 | |
mkdir -p mysite | |
gawk '{ | |
dt=substr($4,2,11); | |
gsub(/\//," ",dt); | |
"date -d \""dt"\" +%Y%m%d"|getline dd; | |
print $0 >> "mysite/mysite.access.log-"dd | |
}' mysite.access.log | |
#将当天部分重命名,避免被覆盖 | |
today=`date +%Y%m%d` | |
todaylog="mysite.access.log-$today" | |
if [ -f "$todaylog" ]; then | |
mv $todaylog "{$todaylog}.head" | |
fi | |
#配置切割日志 | |
cat > /etc/logrotate.d/nginx <<EOD | |
/opt/nginx-1.4.0/logs/mysite.access.log { | |
notifempty | |
nocompress | |
daily | |
dateext | |
olddir /opt/nginx-1.4.0/logs/mysite/ | |
copytruncate | |
create 0600 root root | |
rotate 7 #保存最近一周的日志 | |
prerotate | |
sleep 59 #休眠59秒 | |
endscript | |
postrotate | |
kill -USR1 `cat /opt/nginx-1.4.0/logs/nginx.pid` | |
endscript | |
} | |
EOD | |
#将下面的脚本加入crontab | |
#59 23 * * * /usr/sbin/logrotate -f /etc/logrotate.d/nginx |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
gawk '$1=="2019-07-01"{print NR; exit} {if($1~/^2019\-0/) fname="access.log-" redt($1); print >> fname} function redt(d){gsub("-","",d); return d}' access.log-20190700