【CentOS系统下Tomcat日志管理的最佳实践:自动清理catalina.out】
#!/bin/bash
# 设置变量
LOG_FILE="/opt/tomcat/logs/catalina.out"
MAX_SIZE=$((10 * 1024 * 1024 * 1024)) # 10GB in bytes
# 检查日志文件大小
file_size=$(du -b "$LOG_FILE" | cut -f1)
if [ $file_size -gt $MAX_SIZE ]; then
echo "Log file size ($file_size bytes) exceeds 10GB. Cleaning..."
# 备份原始日志文件,如果不需要备份,请使用#注释以下代码
cp "$LOG_FILE" "${LOG_FILE}.bak"
# 清空原始日志文件
echo "" > "$LOG_FILE"
echo "Log file has been cleaned and backed up to ${LOG_FILE}.bak"
else
echo "Log file size ($file_size bytes) is within limits. No action needed."
fi