腳本示例以下nginx
#!/bin/bash logs_path="/usr/local/openresty/nginx/logs/" pid_path="/usr/local/openresty/nginx/logs/nginx.pid" mv ${logs_path}access.log ${logs_path}access_$(date -d "yesterday" +"%Y%m%d").log gzip ${logs_path}access_$(date -d "yesterday" +"%Y%m%d").log kill -USR1 `cat ${pid_path}`
計劃任務cronbash
00 00 * * * /bin/bash cut_log_day.sh
注:gunzip access.log.gz
解壓後源文件消失,gunzip -c error.log.bak.gz > error.log.bak
解壓後保留源文件ide
此時腳本中也能夠加入刪除一週之前的壓縮日誌spa
#刪除1周前的日誌文件 del_tag=$(date "+%Y%m%d" -d "1 week ago") /bin/rm -rf $logpath/$del_tag.tar.gz 或者使用find查看進行刪除也是能夠的
腳本示例以下rest
#!/bin/bash logs_path="/usr/local/openresty/nginx/logs/" pid_path="/usr/local/openresty/nginx/logs/nginx.pid" date_path=`date +%Y%m%d` #收下這句用於判斷,當時間爲凌晨00:00時,將date_dri設爲昨天 if [ "`date +%H`" = "00" ]; then date_path=`date -d "yesterday" +%Y%m%d` fi #將當前小時減一,由於記錄的是以前的訪問數據 tag=`date +%Y%m%d%H --date="-1 hour"` /bin/mkdir -p ${logs_path}${date_path} mv ${logs_path}access.log ${logs_path}${date_path}/access_${tag}.log gzip ${logs_path}${date_path}/access_${tag}.log kill -USR1 `cat ${pid_path}
添加計劃任務日誌
00 */1 * * * /bin/bash nginx_log_hour.sh
注:gzip ${logs_path}${date_path}/access_${tag}.log
這個是壓縮源文件後並消失,gunzip xx.gz
這個命令是解壓,解壓後源文件消失,如何保留壓縮後的文件話使用gunzip -c error.log.bak.gz > error.log.bak
code