Shell指令碼分割Nginx日誌,並定期清理[不重啟nginx]

來源:互聯網
上載者:User

指令碼如下

#!/bin/bash
#nginx access log segmentation shell script
#日誌目錄
log_dir="/usr/local/nginx/logs/"
cd $log_dir
time=`date +%Y%m%d --date="-1 day"`
#日誌儲存天數
save_days=15
#記錄檔名所處'/'分割的位置,key('/'數+1)
num=6;
 
#此處待最佳化(取得檔案不帶尾碼的檔案名稱)
website=`ls $log_dir*.log | xargs -n 1 | cut -f 1 -d "." | cut -f $num -d "/"`
#日誌目錄下所有的記錄檔為檔案夾,迴圈建立對應的時間檔案
for i in $website
do
    #判斷目錄是否存在
    if [ ! -d $log_dir$i ];then
        mkdir "$i"
    fi
 
    cp $log_dir$i.log $log_dir$i/$i-$time.log
    true>$log_dir$i.log
done
 
find $log_dir -mtime +$save_days -exec rm -rf {} \;
設定crontab,每天淩晨跑一次。

展示: web1.log => web1/web1-20160322.log web1/web1-20160323.log web2.log => web2/web2-20160322.log web2/web2-20160323.log

其他還有 cronolog、logrotate等方式

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.