As an experienced website operations expert, I know that AnQiCMS (AnQiCMS) not only provides efficient content management for enterprises, but also, like all excellent systems, silently records a large amount of operation logs in the background.These log files are crucial for understanding the system's operational status and troubleshooting issues.However, over time, they may also quietly occupy valuable server storage space and even affect system performance.Therefore, regularly cleaning these old log files is an indispensable operation and maintenance link to ensure the long-term stable and efficient operation of AnQiCMS.crontabTools, to automate and regularly clean up old log files of AnQiCMS.

The 'past and present' of log files: why do we need to clean them?

AnQiCMS as an enterprise-level content management system developed based on the Go language generates various logs during operation, such as access logs, error logs, and database operation logs.These log files are like the 'diary' of the system, detailing each request, each operation, and any possible exceptions.They provide first-hand information for us to track user behavior, analyze system bottlenecks, and locate and resolve faults.

However, more is not always better for log files.Over time, the value of old log information gradually decreases, while the storage space it occupies continues to grow.This may not only pose a risk of insufficient disk space, but may also slow down the speed of file system operations in some cases, indirectly affecting the overall performance of AnQiCMS.It is more important that retaining a large amount of logs for a long time may bring challenges in terms of data retention policies or privacy compliance.Therefore, it is particularly important to establish a set of automated cleaning mechanisms to allow these "expired diaries" to leave in time.

Found AnQiCMS log 'treasure'

Before starting the cleanup, we first need to clarify where the AnQiCMS log files are usually stored. Although the AnQiCMS documentation does not explicitly indicate the default log path, according to common Web application deployment habits, log files are usually located in the following locations:

  1. AnQiCMS Installation Directory Belowlogsordata/logsFolder:Many Go language applications will output their logs directly to the directory structure of their own applications.
  2. Linux system standard log path:For/var/log/anqicms/or/var/log/Other subdirectories under it, depending on the packaging method of AnQiCMS or your system administrator's configuration.

To confirm the specific log path, the most reliable method is to log in to your server, enter the installation directory of AnQiCMS, and usels -Rthe command to recursively search, or throughfind . -name "*.log"Search using commands.In addition, you can also check the running configuration or startup script of AnQiCMS, which usually contains configuration items related to the log output location./www/wwwroot/anqicms/logsWe can continue to the next step.

crontab: The secret weapon of time management master

crontabis a tool used in Linux and Unix-like systems for setting up and managing periodic tasks.It allows us to automatically execute commands or scripts at predetermined time intervals, and is the 'behind-the-scenes hero' of server automation operations.crontabThe core is “cron job”, which is a scheduled task. Each cron job consists of two parts: time and the command to be executed.

Time expression consists of five fields, representing:

  • Minutes (0-59)
  • Hours (0-23)
  • Date (1-31)
  • Month (1-12)
  • Day of the week (0-7, where 0 and 7 both represent Sunday)

These fields can be used with an asterisk*(indicating all possible values), comma,(indicating discrete values), hyphen-English indicating range and slash/Combining them to achieve various complex scheduling strategies.

Refinement: Constructing log cleaning commands

With the log path andcrontabthe fundamentals, next we can build the core commands used for cleaning logs. Here we mainly rely on the powerfulfinda command.findThe command can search for files in a specified directory and perform specific operations on the files found.

For example, if we want to delete AnQiCMS/www/wwwroot/anqicms/logsThe directory contains all modifications over 30 days.logThe log files at the end, the command can be written as:

find /www/wwwroot/anqicms/logs -type f -name "*.log" -mtime +30 -delete

Let's break down the various parts of this command:

  • find /www/wwwroot/anqicms/logs: SpecifiesfindCommand starts searching from which directory. Please make sure to replace it with your actual AnQiCMS log path.
  • -type f: InformfindOnly search for files (not directories).
  • -name "*.log": 指定了要查找的文件名模式,这里是所有以结尾的文件。如果您发现 AnQiCMS 的日志文件有其他命名约定(例如.log结尾的文件。如果您发现 AnQiCMS 的日志文件有其他命名约定(例如access.log-2023-01-01orerror.log.old),You may need to adjust this mode, even using multiple-nameparameters through-o(OR) to logically connect.
  • -mtime +30This is the core of the cleaning strategy.It indicates finding all files that were last modified more than 30 days ago.If you want to keep it longer or shorter, you can adjust this number.
  • -delete: This is the most critical step, tellingfindCommand will delete all files that meet the above conditions.

An important security reminder:Before you first execute a command with-deletethe parameter'sfindstrongly recommend that you use the-printparameter substitution-deletePerforming test. ThisfindThe command will list the files it will delete, but will not actually delete them. For example:

find /www/wwwroot/anqicms/logs -type f -name "*.log" -mtime +30 -print

Carefully check the output list to ensure that there are no files you do not want to delete. Confirm that there are no errors before proceeding.-printwith-delete.

Integratecrontab: The journey of automation cleaning

Now, we are ready to clean up the command, it's time to add it tocrontabAutomation has been achieved in.

  1. EditcrontabConfiguration:On your server, open the terminal and enter the following command:

    crontab -e
    

    This will open a text editor (usually)viornano), displaying the current user'scrontabconfiguration. If it is the first time you use it, it may prompt you to select an editor.

  2. Add a cleaning task:Add a new task at the end of the file.To ensure that the log cleaning task is executed during periods of low system load and to avoid frequent sending of the task execution output information via email, we can schedule the task at a fixed time each week and redirect the command output./dev/null: “` 0 3 * * 0 find /www/wwwroot/anqicms/logs -type f -name “*.log” -mtime +30 -delete > /dev/null 2>&1