SoFunction
Updated on 2025-04-14

Implementation method of using Bash script to automatically clean Nacos log files

introduction

In modern distributed systems, Nacos is a core component of service discovery, configuration management and dynamic service management, and its log file management is particularly important. As the system runs, log files will continue to accumulate and occupy a large amount of disk space. If it is not cleaned up in time, it may lead to insufficient disk space, affect system performance and even lead to service interruption. This article will introduce how to automatically clean Nacos' log files through a simple Bash script, keep logs for the last 30 days, and log the logs of the cleaning operation.

Background and requirements

During Nacos running, log files usuallyaccess_log.date.logFormat naming, e.g.access_log.. Over time, these log files will accumulate and occupy a lot of disk space. In order to ensure the stable operation of the system, we need to clean these log files regularly and keep the logs for the last 30 days for subsequent analysis and troubleshooting.

Script design and implementation

In order to achieve the requirement of automated cleaning of log files, we designed a Bash script, whose main functions include:

  • Cleaning of log files: Delete log files for more than 30 days.
  • Logging: Record the detailed information of each cleaning operation into an independent log file to facilitate subsequent audits and troubleshooting.

The following is a detailed implementation of the script:

#!/bin/bash

# Output log locationlogFile="/data/nacos/nacos/bin/nacos_del_access.log"
# Keep 30 days of logdateLog=$(date --date="-30 days" +%Y-%m-%d)

# The specific position can be adjustedlogDir="/data/nacos/nacos/bin/logs"

# Check whether the log file existsif [ ! -f "${logFile}" ]; then
    touch "${logFile}"
    echo "Created log file: ${logFile}"
fi

# traverse all files in the log directoryfor file in "${logDir}/access_log."*".log"; do
    # Extract the date part of the file name    fileName=$(basename "$file")
    fileDate=$(echo "$fileName" | grep -oE '[0-9]{4}-[0-9]{2}-[0-9]{2}')

    # Check if the file name contains a date    if [ -n "$fileDate" ]; then
        # Compare dates, if the file date is earlier than or equal to a date 30 days ago, delete it        if [ $(date -d "$fileDate" +%s) -le $(date -d "$dateLog" +%s) ]; then
           rm -f "$file"
           curDate=$(date --date='0 days ago' "+%Y-%m-%d %H:%M:%S")
           echo "[${curDate}] Deleted file: ${file}" >> "${logFile}"
        fi
    fi
done

Script parsing

  • Log file path and retention period

    • logFile: Defines the storage path for cleaning operation logs.
    • dateLog: Calculate the date 30 days ago to determine whether the log file needs to be deleted.
  • Log Directory

    • logDir: Defines the storage path of the Nacos log file.
  • Log file inspection and creation

    • If the cleaning operation log file does not exist, create the file and record the creation information.
  • Iterate over log files

    • useforLoop through all the log directoryaccess_log.Start with.logThe ending file.
  • Extract dates in filenames

    • usegrepand regular expressions extract the date part in the file name.
  • Date comparison and file deletion

    • Compare the date in the file name to a date 30 days ago, and delete the file if the file date is earlier than or equal to a date 30 days ago.
    • Log details of the deletion operation to the cleanup operation log file.

How to use

Script deployment

  • Save the script as a file, e.g.clean_nacos_logs.sh
  • Grant script execution permissions:
chmod +x clean_nacos_logs.sh

Regularly executed

  • Can be passedcronTimed tasks execute scripts regularly. For example, perform at 2 a.m. every day:
crontab -e
  • Add the following:
0 2 * * * /path/to/clean_nacos_logs.sh
  • Parameter adjustment

    • If you need to adjust the number of retained log days, you can modify thedateLogvariable.
    • If the log file storage path is different, you can modify itlogDirandlogFilevariable.

Summarize

Through the above Bash script, we can realize the automated cleaning of Nacos log files to ensure the reasonable use of system disk space, while retaining the necessary log files for subsequent analysis. This script is simple and easy to use and can be adjusted and expanded according to actual needs. For systems that need to manage a large number of log files, this automated cleaning method can significantly improve operation and maintenance efficiency and reduce the complexity of system maintenance.

The above is the detailed content of the implementation method of using Bash scripts to automatically clean Nacos log files. For more information about cleaning Nacos log files in Bash scripts, please pay attention to my other related articles!