ngix日誌切割

來源:互聯網
上載者:User

#!/bin/bash
#function:cut nginx log files for lnmp v0.5 and v0.6
#author: http://lnmp.org

#set the path to nginx log files
log_files_path="/home/wwwlogs/"
log_files_dir=${log_files_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")
#set nginx log files you want to cut
log_files_name=(access vpser licess)
#set the path to nginx.
nginx_sbin="/usr/local/nginx/sbin/nginx"
#Set how long you want to save
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

下面是我根據上面的提示寫的一個清理tomcat容器的一個指令碼:

#!/bin/bash
#Function:zip the tomcat containers logs by month
#Author:zhuying
#

#Get the tomcat config
grep "^tomcat" /nfs/sor/share/app.properties |cut -d\: -f2|awk '{print $1,$3}'|sort|uniq > tomcat_info

#set catalina log files you want to deal with
log_files_name=(access catalina)
log_files_num=${#log_files_name[@]}

while read ip path
do
 #for i in {1..10};do
 for ((i=0;i<$log_files_num;i++));do
  month=`date -d "-1 month" +%Y-%m`
  echo ++Start++++++++++++++++++$ip $path==========================
  echo "`date` $month"
  ssh $ip "cd $path;cd ../logs;find . -name '${log_files_name[i]}.$month*.log' -size +10240 -print -exec gzip {} \;" </dev/null
  ssh $ip "cd $path;cd ../logs;find . -name 'catalina.$month*.out' -size +10240k -print -exec gzip {} \;" </dev/null
  echo "Now start:zip the month $month logs"
  ssh $ip "cd $path;cd ../logs;find . -name '${log_files_name[i]}.$month*' -print -exec zip -r ${log_files_name[i]}.$month.zip {} \;" </dev/null
  ssh $ip "cd $path;cd ../logs;find . -name '${log_files_name[i]}.$month*.log' -print -exec rm {} \;" </dev/null
  ssh $ip "cd $path;cd ../logs;find . -name '${log_files_name[i]}.$month*.log.gz' -print -exec rm {} \;" </dev/null
  echo ++Over++++++++++++++++++$ip $path==========================
 done

done < tomcat_info >> /opt/log/tomcat/container/tomcatcontain.log.`date +%F`

聯繫我們

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