How to use `crontab` to regularly clean up the old log files generated by AnQiCMS?

Calendar 👁️ 70

As an experienced website operations expert, I know that AnQiCMS (AnQiCMS) provides efficient content management for enterprises while, like all excellent systems, it silently records a large amount of operation logs in the background.These log files are crucial for us to understand the system's operation and troubleshoot issues.However, over time, they will 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 stability and efficient operation of AnQiCMS.Today, let's talk about how to cleverly use the Linux system incrontabTool, to implement the automation and regular cleaning of old log files in AnQiCMS.

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

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

However, log files are not the more, the better. As time goes by, the value of old log information gradually decreases, while the storage space it occupies continues to grow.This could lead to the risk of insufficient disk space and may slow down 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 pose challenges in terms of data retention policy or privacy compliance.Therefore, establishing an automated cleaning mechanism to make these "expired diaries" leave in a timely manner is particularly important.

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 state the default log path, according to common Web application deployment practices, log files are usually located in the following locations:

  1. AnQiCMS installation directory underlogsordata/logsfolder:Many Go language applications will output their logs directly to the directory structure of their own applications.
  2. Linux system standard log path:As/var/log/anqicms/or/var/log/Other subdirectories under it, depending on the packaging method of AnQiCMS or the configuration of your system administrator.

To confirm the specific log path, the safest method is to log in to your server, enter the installation directory of AnQiCMS, and usels -Rthe recursive search command, or throughfind . -name "*.log"Search for commands. In addition, you can also refer to the AnQiCMS operation configuration or startup script, usually there will be configuration items about the log output location.Once the exact path to the log file has been found, for example, assuming it is/www/wwwroot/anqicms/logsWe can move on to the next step.

crontabThe secret weapon of time management master

crontabIs a tool used to set and manage periodic tasks in Linux and Unix-like systems.It allows us to automatically execute commands or scripts at predetermined intervals, being 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.

The time expression is composed of five fields representing:

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

These fields can use an asterisk*(indicating all possible values), comma,(indicating discrete values), hyphen-Range represented by and slash/Combining the step represented to achieve various complex scheduling strategies.

Refined to the extreme: Build log cleaning commands

With the log path andcrontabThe basic knowledge, next we can build the core commands for cleaning logs. Here we mainly rely on the powerfulfindcommand.findThe command can search for files in the specified directory and perform specific operations on the files found.

For example, if we want to delete AnQiCMS/www/wwwroot/anqicms/logsAll files modified more than 30 days ago in the directory.logLog files ending with, 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: SpecifiesfindThe command starts searching from which directory. Please replace it with the actual AnQiCMS log path.
  • -type f: TellfindOnly search for files (not directories).
  • -name "*.log": Specified the filename pattern to search for, which is all files ending with.log. If you find that the AnQiCMS log files have other naming conventions (such asaccess.log-2023-01-01orerror.log.old),You may need to adjust this pattern, even using multiple-nameParameters pass-o(OR) Connect logically.
  • -mtime +30This is the core of the cleaning strategy. It indicates the search for all files that were last modified 30 days ago.If you want to keep it longer or shorter, you can adjust this number.
  • -delete: This is the most critical step, tellingfindThe command will delete all files that meet the above conditions.

An important security reminder:Before you first execute the command with-deleteThe parameter'sfindstrongly recommend that you use the-printparameter replacement first-deletePerform a test. Like thisfindThe command will only 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

Please check the output list to ensure there are no files you do not want to delete. Confirm that it is correct before proceeding to-printReplace-delete.

integratecrontab: Automated cleaning journey

Now, we are ready to prepare the cleanup command, it's time to add it tocrontabto achieve automation.

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

    crontab -e
    

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

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

Related articles

AnQiCMS `crontab` error message提示 “command not found”, how to solve?

## AnQiCMS `crontab` error提示 “command not found” in-depth analysis and solution As an expert in website operation for many years, I know how crucial stable automated tasks are when managing a high-efficiency content management system like AnQiCMS.AnQiCMS leverages its high-performance architecture in the Go language and rich features, providing us with powerful support for content publishing, SEO optimization, and even scheduled tasks.

2025-11-06

How to update all related `crontab` tasks at once when upgrading AnQiCMS multi-site in bulk?

In the multi-site operation of AnQi CMS, efficiency and stability are always the core pursuit.As a CMS system developed based on the Go language, AnQiCMS has won the favor of many operators with its excellent performance and multi-site management capabilities.However, when faced with multi-site batch upgrades, how to巧妙ly handle the resulting `crontab` task updates to ensure a smooth transition for all sites, this is undoubtedly a problem that many operation experts need to ponder.Today, let's delve into this topic and provide you with a set of effective strategies.###

2025-11-06

Who is the default user for `crontab` tasks in AnQiCMS? How to modify it?

As an experienced website operations expert, I am well aware of the importance of AnQiCMS as an efficient content management system in daily operations.The configuration of scheduled tasks (`crontab`) is a key factor in ensuring the stable operation of the system, timely publication of content, and timely update of data.For many AnQiCMS users, especially those who are new to the Linux environment, they may be puzzled about the execution user of the `crontab` task.Today, let's delve deeply into this issue.

2025-11-06

How to add custom environment variables in AnQiCMS `crontab` tasks and make them effective in `crontab`?

Good, as an experienced website operation expert, I am happy to analyze for you how to add and take effect custom environment variables in the `crontab` task of AnQiCMS.This can make your scheduled tasks more flexible, and it can also better meet the needs of automated operations and specific environmental configurations.--- ## Unlocking Automation Potential: Adding Custom Environment Variables to AnQiCMS's `crontab` Tasks AnQiCMS, with its efficient and customizable features, has become a powerful assistant for many small and medium-sized enterprises and content operation teams

2025-11-06

AnQiCMS `crontab` task execution failed, how to get detailed error reports?

As an experienced website operations expert, I know how anxious operators can be when automated tasks - especially core scheduling tools like `crontab` - encounter problems.AnQiCMS is a content management system known for its efficiency and stability, although it is well-designed, various factors in the external environment during actual deployment and operation may still cause the associated `crontab` task to fail.How to quickly and accurately obtain detailed error reports at this time is the key to solving the problem.Today, let's delve deeply into, when your

2025-11-06

Does AnQiCMS need to set `crontab` separately when deployed in a Docker container?

As an experienced website operation expert, I know that how to run applications efficiently and stably in increasingly complex deployment environments is a major concern for everyone.AnQiCMS (AnQiCMS) with its efficient Go language, concise architecture, and rich features, has become the preferred choice for many small and medium-sized enterprises and content operation teams.When combined with containerization technology Docker, a common and critical issue emerges: 'Do you need to set up `crontab` separately when AnQiCMS is deployed in a Docker container?'

2025-11-06

How to set up email notifications in AnQiCMS `crontab` tasks to receive service status updates in a timely manner?

How to set up email notifications in the AnQiCMS `crontab` task to keep abreast of service status?AnQiCMS with its efficient and concise Go language architecture, provides us with a stable and reliable content management service.However, even the strongest system needs our careful attention. As a website operations expert, I fully understand the importance of timely monitoring the system's operating status.Imagine if our AnQiCMS website were to crash unexpectedly in the middle of the night, and we were completely unaware of it, this would undoubtedly cause damage to the user experience and brand image. At this time

2025-11-06

How to quickly update the `crontab` task after the `start.sh` script path changes in AnQiCMS?

As an experienced website operation expert, I know that AnQiCMS, an enterprise-level content management system developed based on the Go language, occupies a place in the hearts of many operators with its high efficiency, lightweight, and high concurrency characteristics.It brings great convenience to small and medium-sized enterprises and content operation teams, but even the most excellent system cannot do without meticulous operation and maintenance management.In daily operations and maintenance, we sometimes encounter situations where we need to adjust the path of the AnQiCMS core startup script `start.sh`. When this path changes

2025-11-06