tomcatLogZip.sh文件内容
#!/bin/sh
#清理30天以前的日志
find /usr/local/edsweb/data/log/ -mtime +30 -name "*.log*" -exec rm -rf {} \;
#执行时间设置为每日23:55跑,压缩转储当天的日志
#当天日期(不是昨天)
yesterday=`date +%Y-%m-%d`
#tomcat日志路径
dir=/home/eds/tomcat-8.5.31-8080/logs
#原日志文件名
file=catalina.out
#复制后的文件名
datefile=catalina."$yesterday".out
#压缩后日志文件名
zipFile="$datefile".tar.gz
if [ -s "$dir"/"$file" ]; then
cd ${dir}
echo ">>start copy ${dir}/${file}..."
cp $file $datefile
echo ">>copy finished!"
echo > $file
if [ -s "$dir"/"$datefile" ]; then
cd ${dir}
echo ">>start zip ${dir}/${datefile}..."
tar -czvPf ${zipFile} ${dir}/${datefile}
echo ">>zip finished."
rm ${dir}/${datefile}
echo ">>file deleted."
exit
else
echo ">>no have it! ${dir}/${datefile},stop zip!"
fi
exit
else
echo ">>no have it! ${dir}/${file},stop copy!"
fi
操作说明:
创建目录:mkdir /home/eds/logsh
将tomcatLogZip.sh 上传到 /home/eds/logsh
配置crontab : crontab -e
增加配置 : 55 23 * * * sh /home/eds/logsh/tomcatLogZip.sh