How does the startup script ensure that AnQiCMS automatically restarts after an unexpected stop?
AnQiCMS is a modern content management system developed based on the Go language, and its stability and high availability are the core guarantees of website operation.In the operation of a website, the background service process may unexpectedly stop due to various unpredictable reasons, such as exhaustion of system resources, program errors, external attacks, etc.To ensure the continuity of the website service, AnQiCMS has designed an automated mechanism to monitor and restart its core processes.
The core of this automatic reboot mechanism lies in the system scheduling tasks configured on the Linux server (usually throughcrontabservice implementation) and a specially designed startup scriptstart.shCollaborative work. In this way, AnQiCMS achieves self-monitoring and fault recovery capabilities.
The principle of AnQiCMS automatic restart implementation
When AnQiCMS process stops unexpectedly, its automatic restart feature mainly depends on the following steps and components:
First, on the server'scrontabThe scheduled task is configured to periodically execute a specific script file. According to the AnQiCMS deployment documentation, the script is usually located in the application installation directory.start.shThe most common configuration is to letcrontabExecute this every minutestart.shscript to ensure 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 if necessary. The script usually defines two key variables:BINNAMEandBINPATH.BINNAMEIs the executable file name of AnQiCMS (default asanqicms),whileBINPATHIt is the installation path of AnQiCMS. The introduction of these variables makes the script highly configurable, convenient for users to adjust according to actual deployment situations.
The script is executed throughps -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\>'Filter out process lines containing the word "anqicms"(\<and\>Ensure that the match is a complete word to avoid affecting other processesgrep -v grepThen it will filter outgrepCommand its own process to prevent interference, finallywc -lCount the number of lines matched, i.e., the number of AnQiCMS processes.
If the script detects that the number of AnQiCMS processes is 0, that isexists -eq 0then 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 is used to:
cd $BINPATH: First, switch to the AnQiCMS installation directory to ensure the program can start in the correct environment.nohup $BINPATH/$BINNAME: Use.nohupCommand to start the AnQiCMS executable file.nohupThe function is to allow the program to run in the background, and it will not stop even after the user logs out or closes the terminal.>> $BINPATH/running.log 2>&1Redirects the program's standard output (stdout) and standard error (stderr) torunning.logIn the file. This is very important for subsequent fault diagnosis and running status monitoring.&Finally, put the entire startup command into the background to run, not occupying the currentcrontabprocess resources.
BycrontabA periodic check every minute, AnQiCMS can achieve nearly real-time process monitoring and automatic repair. Once the main program is unexpectedly terminated, the scheduled task will detect this situation within at most one minute and execute immediatelystart.shThe script restarts the service. This mechanism greatly enhances the reliability and stability of the AnQiCMS website, minimizing service downtime and ensuring the continuous availability of website content.
Frequently Asked Questions (FAQ)
Q1: If the AnQiCMS process stops unexpectedly and the restart script fails to start the program, how should I troubleshoot?A1: If the script restart fails to start AnQiCMS, first you should checkrunning.logThe file records the output and any error information of the AnQiCMS program when it 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.You can still manually execute it in the command linecd $BINPATH && $BINPATH/$BINNAMEtry 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 a separate port.When configuring, you need to create a separate installation directory for each instance and modify itsconfig.jsonFiles are specified with different ports and modifiedstart.shin the scriptBINNAMEAnd if the executable file has been renamedBINPATHThe variable should match the actual path of the instance. Finally, add a separate scheduled task entrycrontabfor each instance, pointing to its respectivestart.shscript.
Q3: Can the frequency of the automatic restart check be adjusted? If I want it to check every five minutes instead of every minute, how should I set it?A3: The frequency of automatic restart check 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 editcrontabConfigure (for example bycrontab -ecommand), find the line corresponding to the AnQiCMS startup script.*/1 * * * *is modified to*/5 * * * *Just do it.*/5Indicates every 5 time units, i.e., every 5 minutes. Please set this frequency reasonably based on your needs for service recovery speed and the use of server resources.