From fd58cd5eac3be9d9dc99f877224f47d0ed3b97df Mon Sep 17 00:00:00 2001 From: shroominic Date: Mon, 30 Jun 2025 12:27:43 +0000 Subject: [PATCH] auto prune logs --- scripts/auto_update.sh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/scripts/auto_update.sh b/scripts/auto_update.sh index 7a5a0d8c..0071c04f 100755 --- a/scripts/auto_update.sh +++ b/scripts/auto_update.sh @@ -7,12 +7,26 @@ REPO_DIR="${REPO_DIR:-/home/ubuntu/proxy}" LOG_FILE="${LOG_FILE:-/home/ubuntu/proxy/update.log}" LOCK_FILE="${LOCK_FILE:-/tmp/proxy_update.lock}" +MAX_LOG_LINES="${MAX_LOG_LINES:-10000}" # Maximum number of log lines to keep # Function to log messages with timestamp log_message() { echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE" } +# Function to prune log file to keep only recent entries +prune_logs() { + if [ -f "$LOG_FILE" ]; then + local line_count=$(wc -l < "$LOG_FILE" 2>/dev/null || echo 0) + if [ "$line_count" -gt "$MAX_LOG_LINES" ]; then + # Create a temporary file with only the last MAX_LOG_LINES lines + tail -n "$MAX_LOG_LINES" "$LOG_FILE" > "${LOG_FILE}.tmp" + mv "${LOG_FILE}.tmp" "$LOG_FILE" + log_message "Log file pruned. Kept last $MAX_LOG_LINES lines." + fi + fi +} + # Function to cleanup on exit cleanup() { rm -f "$LOCK_FILE" @@ -73,3 +87,6 @@ else fi log_message "Update check completed successfully" + +# Prune logs after each run to prevent unlimited growth +prune_logs