nginx日誌分割 for linux_nginx

來源:互聯網
上載者:User

使用方法,先將以下指令碼儲存為 cutlog.sh,放在/root 目錄下,然後給予此指令碼執行的許可權

複製代碼 代碼如下:

chmod +x cutlog.sh

然後使用crontab -e 將此指令碼加入到計劃任務中,

複製代碼 代碼如下:

00 00 * * * /bin/bash /root/cutlog.sh

讓此指令碼每天淩晨0點0分執行。

複製代碼 代碼如下:

#!/bin/bash
#function:cut nginx log files shell
#設定您的網站訪問日誌儲存的目錄,我的統一放在了/home/wwwlogs目錄下
log_files_path="/home/wwwlogs/"
log_files_dir=${log_files_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")
#設定你想切割的nginx記錄檔名稱,比如設定的記錄檔名是52listen.com.log 的話,那這裡直接填寫 52listen.com 即可
log_files_name=(52listen.com access)
#設定nginx執行檔案的路徑。
nginx_sbin="/usr/local/nginx/sbin/nginx"
#設定你想儲存的日誌天數,我這裡設定的是儲存30天之前的日誌
save_days=30
############################################
#Please do not modify the following script #
############################################
mkdir -p $log_files_dir
log_files_num=${#log_files_name[@]}
#cut nginx log files
for((i=0;i<$log_files_num;i++));do
mv ${log_files_path}${log_files_name[i]}.log ${log_files_dir}/${log_files_name[i]}_$(date -d "yesterday" +"%Y%m%d").log
done
#delete 30 days ago nginx log files
find $log_files_path -mtime +$save_days -exec rm -rf {} \;
$nginx_sbin -s reload

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.