AnQiCMS as a modern content management system developed based on the Go language, its stability and high availability are the core guarantees for website operation.In the actual operation of the website, the background service process may unexpectedly stop due to various unpredictable reasons, such as insufficient system resources, program errors, external attacks, and so on.To ensure the continuity of the website service, AnQiCMS has designed a set of automated mechanisms to monitor and restart its core processes.
The core of this automatic restart mechanism lies in the system scheduled tasks configured on the Linux server (usually throughcrontabservice implementation) and a specially designed startup scriptstart.shThe collaborative work. By this means, AnQiCMS achieves self-monitoring and fault recovery capabilities.
The implementation principle of AnQiCMS's automatic restart
When the AnQiCMS process stops unexpectedly, its automatic restart function mainly relies on the following steps and components:
First, on the server,crontabThe scheduled task is configured to periodically execute a specific script file. According to the AnQiCMS deployment document, this script is usually located in the application installation directory.start.shThe most common configuration is tocrontabExecute this task every minutestart.shSo that the system can quickly respond to any process interruption.
Secondly,start.shThe script contains the logic to check the running status of the AnQiCMS main program and trigger a restart when necessary. The script usually defines two key variables:BINNAMEandBINPATH.BINNAMEIs the executable file name of AnQiCMS (default isanqicms), whileBINPATHThen it is the installation path of AnQiCMS. The introduction of these variables makes the script have good configurability, convenient for users to adjust according to the actual deployment situation.
The script executesps -ef | grep '\<anqicms\>' | grep -v grep | wc -lThis command is used to check if the AnQiCMS process is running. The working principle of this command is:ps -efIt will list all the running processes on the system,grep '\<anqicms\>'It will filter out process lines containing the word "anqicms"\<and\>Ensure that the match is a complete word to avoid mistakenly affecting other processes"),grep -v grepThen it will filter outgrepCommanding its own process to prevent interference, finallywc -lCounting the number of matched lines, that is, the number of AnQiCMS processes.
If the script detects that the number of AnQiCMS processes is 0, that isexists -eq 0, it indicates that the AnQiCMS program has stopped running. In this case,start.shthe startup command will be executed immediately:cd $BINPATH && nohup $BINPATH/$BINNAME >> $BINPATH/running.log 2>&1 &This command's function is:
cd $BINPATHFirst, switch to the installation directory of AnQiCMS, make sure the program can start in the correct environment.nohup $BINPATH/$BINNAME: UsenohupCommand to start the AnQiCMS executable file.nohupThe function is to allow the program to run in the background, and the program will not stop even after the user logs out or closes the terminal.>> $BINPATH/running.log 2>&1Redirect the standard output (stdout) and standard error (stderr) torunning.logThe file. This is very important for subsequent fault diagnosis and running status monitoring.&Finally, execute the entire startup command in the background without occupying the currentcrontabprocess resources.
PasscrontabA periodic check every minute, AnQiCMS can achieve near real-time process monitoring and automatic repair. Once the main program is unexpectedly terminated, the scheduled task will detect this situation within a maximum of one minute and execute immediatelystart.sh脚本来重新启动服务。This mechanism greatly enhances the reliability and stability of the AnQiCMS website, minimizes service downtime, and ensures the continuous availability of website content.
Common Questions and Answers (FAQ)
Q1: If the AnQiCMS process stops unexpectedly and the restart script fails to start the program, how should I conduct an investigation?A1: If the script restart fails to start AnQiCMS, first checkrunning.logFile.This file records the output and any error information when the AnQiCMS program starts. These logs are crucial for diagnosing the cause of startup failure.Common failure reasons may include port occupation, database connection issues, insufficient file permissions, or configuration file errors, etc.cd $BINPATH && $BINPATH/$BINNAMEto try to start it and observe the detailed error information output by the terminal.
Q2: Can I install multiple AnQiCMS instances on a single server and use the automatic restart feature for all of them?A2: Yes, AnQiCMS supports installing multiple instances on a single server and configuring automatic restarts for each instance.Each AnQiCMS instance requires an independent port.config.jsonFiles are specified with different ports and modifiedstart.shscript, make sure it points to the correct directory of the AnQiCMS executable file, and that the directory containsBINNAMEIf the executable file has been renamedBINPATHThe variable matches the actual path of the instance. Finally, add an independent scheduled task entrycrontabpointing to their respective start.shscript.
Q3: Can the frequency of automatic restart checks be adjusted? If I want it to check every five minutes instead of every minute, how should I set it?A3: The check frequency for automatic restart is fully configurable. It is determined bycrontabthe time expression in the scheduled task. To adjust the check frequency from every minute to every five minutes, you need to editcrontabConfiguration (for example, throughcrontab -ecommand), find the line corresponding to the AnQiCMS startup script.*/1 * * * *Change to*/5 * * * *.*/5Represents every 5 time units, i.e., every 5 minutes. Please set this frequency reasonably based on your needs for service recovery speed and the usage of server resources.