ngix日誌切割

#!/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`


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章