As an expert who deeply understands the operation of AnQiCMS, I am well aware of the importance of system stability for content management.AnQiCMS as an enterprise-level content management system developed based on the Go language has high performance and reliability as one of its core advantages.Even though the system itself is robustly designed, but the server environment is complex and variable, unexpected process termination may still occur.How to ensure that AnQiCMS can quickly recover the service in this case is a key point that website operators must pay attention to.crontabSchedule task, implements an automatic restart mechanism after process unexpected termination.

AnQiCMS process guardian and automatic recovery mechanism

The automatic pull-up mechanism of AnQiCMS mainly depends on the Linux/Unix system'scrontabService, this is a tool used for scheduling and executing periodic tasks.The core idea is: regularly check the running status of the AnQiCMS main process, and if the process is not running, automatically execute the startup command to bring it up.This ensures the continuous availability of AnQiCMS services and greatly reduces the burden of manual intervention by operations personnel.

crontabconfiguration of the scheduled tasks

In the deployment documentation of AnQiCMS, it explicitly guides users to configure one when deploying on a Linux server.crontabSchedule task. This scheduled task will execute a script namedstart.shTypicalcrontabentry configuration is as follows:

*/1 * * * * /www/wwwroot/anqicms.com/start.sh

The meaning of this command is: every minute*/1 * * * *),The system will execute the specified path understart.shscript. Path/www/wwwroot/anqicms.com/start.shis in the AnQiCMS installation directorystart.shThe example path of the script, which needs to be adjusted according to the installation location of AnQiCMS during actual deployment.

start.shThe working principle of the script

start.shThe script is the core of implementing the automatic pull-up logic.It does not start AnQiCMS unconditionally, but first checks if the main process of AnQiCMS is running, and only executes the start operation when the process does not exist.This effectively avoids the problem of repeated startup during the normal operation of AnQiCMS, leading to multiple instances running or waste of resources.

The simplified analysis of the script content is as follows:

#!/bin/bash
BINNAME=anqicms
BINPATH=/www/wwwroot/anqicms

# 检查进程是否存在
exists=`ps -ef | grep '\<anqicms\>' |grep -v grep |wc -l`

# 如果进程不存在(即计数为0),则启动AnQiCMS
if [ $exists -eq 0 ]; then
    cd $BINPATH && nohup $BINPATH/$BINNAME >> $BINPATH/running.log 2>&1 &
fi

The key steps of this script are as follows:

Firstly,BINNAMEandBINPATHThe variable defines the executable file name of AnQiCMS (usuallyanqicms) and its installation path. These variables ensure that the script can locate and operate the correct program.

Next,ps -ef | grep '\<anqicms\>' | grep -v grep | wc -lThis combined command, the script can accurately check if the process namedanqicmsis running.

  • ps -efList all running processes.
  • grep '\<anqicms\>'Filter the lines containing the keyword "anqicms", where\<and\>Is a word boundary, ensuring that only the complete 'anqicms' process name is matched, not other strings containing 'anqicms'.
  • grep -v grepExclude it.grepCommand itself generates process lines, avoid misjudgment.
  • wc -lCount the number of lines finally filtered out, i.e., the number of AnQiCMS processes.

Finally,if [ $exists -eq 0 ]Determine if the process count is 0 (i.e., anqicmsProcess not running), then execute the start command:cd $BINPATH && nohup $BINPATH/$BINNAME >> $BINPATH/running.log 2>&1 &.

  • cd $BINPATH: Switch to the installation directory of AnQiCMS, which is crucial for ensuring the program can correctly find its dependent files.
  • nohup $BINPATH/$BINNAME: UsenohupCommand to start the AnQiCMS executable file.nohupThe purpose is to allow the program to continue running even after the user exits the terminal, preventing the process from terminating due to the disconnection of the SSH session.
  • >> $BINPATH/running.log 2>&1Redirect the standard output and standard error of the program torunning.logthe file. This is very helpful for subsequent fault diagnosis and log analysis.
  • &:Run the program in the background and free up the current terminal.

Summary and advantages

Through the abovecrontabWithstart.shThe collaborative work of scripts, AnQiCMS implements a simple and efficient process guardian mechanism. Once the AnQiCMS process is unexpectedly terminated for any reason (such as program crash, memory overflow, etc.),crontabThis situation will be detected within a maximum of one minute, and the action will be executed immediately.start.shScript, which will automatically launch AnQiCMS and restore the website service.

The advantage of this mechanism is:

  • High availability:The service interruption time was significantly reduced, improving the website's continuous online capability.
  • Deployment is straightforward:It requires only a few simple commands to configurecrontaband scripts, without the need for complex process management tools.
  • Resource Efficient:Processes are only started when necessary, avoiding unnecessary resource consumption.
  • Easy to Maintain:Script logic is clear, making it easy to understand and adjust according to actual needs.

This mechanism is an important guarantee for AnQiCMS to maintain stable operation in the production environment, and it is also the foundation for operation personnel to manage content with peace of mind and focus on business expansion.


Common Questions (FAQ)

1. Ifstart.shIf the script itself fails to execute, will AnQiCMS still be launched?

Ifstart.shThere is an error in the internal logic of the script (for example,BINPATHorBINNAMEIf the setting is incorrect or there is a permission issue that prevents the startup command from being executed, then AnQiCMS will not be started correctly.crontabWill continue to execute the script every minute, but each execution will fail due to an internal error in the script. You can checkrunning.logorcrontabthe logs (usually in/var/log/syslogor/var/log/cron) to troubleshootstart.shThe execution status and error information of the script.

How do I manually stop the AnQiCMS process to preventcrontabit from being repeatedly started?

To manually stop AnQiCMS and preventcrontabLift it up immediately, you need to edit firstcrontabConfigure, comment out or delete the startup task of AnQiCMS. Executecrontab -eCommand, find the startup line of AnQiCMS in the open editor (for example*/1 * * * * /www/wwwroot/anqicms.com/start.sh), and add it before#Comment the symbol out, then save and exit. After that, you can use the AnQiCMS installation directory understop.shscript or manually find andkillthe AnQiCMS process (usingps -ef | grep anqicmsLocate the process ID, thenkill -9 <PID>). After completing maintenance or debugging, you can then cancelcrontabthe comment of the task.

3.nohupand&Instart.shWhat is its important role in the script?

nohupand&It is the key to ensure the stable operation of AnQiCMS as a background service.

  • nohupIts function is to ignore the HUP (hang-up) signal.This means that even if you close the terminal session for starting AnQiCMS (for example, the SSH connection is disconnected), the AnQiCMS process will not be terminated due to this, and it can continue to run in the background.
  • &This symbol puts the command into the background for execution.This means the script will immediately return to the command prompt without waiting for the AnQiCMS program to finish executing, allowing the script to continue processing subsequent commands (if any) or exit directly, while the AnQiCMS process runs independently in the background.The combination of these two commands allows AnQiCMS to run independently as a daemon for a long time without user sessions, which is a standard and necessary configuration for any web service.