Nginx日志切割脚本:

#!/bin/bash

#############################################################################
logs_path="/usr/local/nginx/logs"
pid_path="/var/run/nginx.pid"
backup_dir="/data/log/nginx"
[ ! -d $backup_dir ] && mkdir -p $backup_dir
mv ${logs_path}/access.log ${backup_dir}/access_$(date -d "yesterday" +"%F").log gzip ${backup_dir}/access_$(date -d "yesterday" +"%F").log && echo "nginx log sucess" kill -USR1 `cat ${pid_path}`

Java日志切割脚本:

#!/bin/bash
#This script run at 00:03 in cron
logs_path="/usr/local/tomcat7/logs"
logs_dir="/data/log/tomcat/"
logdate=`date -d "yesterday" +"%Y-%m-%d"`
for i in $logs_path
do
echo "...................", $i
Hostname=`hostname`
yesterday=`date -d "yesterday" +"%Y%m%d"`
[ ! -e ${logs_dir}$(date +"%Y%m") ] && mkdir -p $logs_dir$(date +"%Y%m")
cat $i/catalina.out > $logs_dir$(date +"%Y%m")/catalina.out_"$Hostname"_"$yesterday".log
cat $i/localhost_access_log.$logdate.txt > $logs_dir$(date +"%Y%m")/localhost_access_log_"$Hostname"_"$yesterday".log echo ""> $i/catalina.out
cd $logs_dir$(date +"%Y%m") && gzip catalina.out_"$Hostname"_"$yesterday".log && echo "java gzip sucess..." done

2.一台机器上面跑两个tomcat应用的日志切割方法:

#######################################################################################

#!/bin/bash
#This script run at 00:03 in cron
#####################################################################################
logs_path="/usr/local/tomcat-App1/logs/ /usr/local/tomcat-App2/logs"
logs_dir="/data/log/tomcat/"
logdate=`date -d "yesterday" +"%Y-%m-%d"`
pro="App1 App2"
for i in $logs_path
do
echo "...................", $i
Hostname=`hostname`
yesterday=`date -d "yesterday" +"%Y%m%d"`
[ ! -e ${logs_dir}$(date +"%Y%m") ] && mkdir -p $logs_dir$(date +"%Y%m")
for m in $pro
do
cat $i/catalina.out > $logs_dir$(date +"%Y%m")/cata_"$m"_"$Hostname"_"$yesterday".log
cat $i/localhost_access_log.$logdate.txt > $logs_dir$(date +"%Y%m")/localhost_access_"$m"_"$Hostname"_"$yesterday".log
done
done
echo ""> $i/catalina.out
cd $logs_dir$(date +"%Y%m") && gzip cata_*_"$Hostname"_"$yesterday".log && echo "java gzip sucess..."

加入定时任务:

*/59 * * * * /usr/sbin/ntpdate cn.pool.ntp.org>/dev/null 2>&1
0 0 * * * /bin/sh /opt/bin/crontab/cronolog.sh 2>&1
10 0 * * * /bin/sh /opt/bin/crontab/nginx_log.sh /dev/null 2>&1

 

04-30 01:33