How to use `crontab` to regularly clean up the old log files generated by AnQiCMS?
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:
- AnQiCMS installation directory under
logsordata/logsfolder:Many Go language applications will output their logs directly to the directory structure of their own applications. - 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.
Edit
crontabConfiguration:On your server, open the terminal and enter the following command:crontab -eThis will open a text editor (usually
viornano), displaying the current user'scrontabconfiguration. If it is the first time you are using it, it may prompt you to select an editor.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