How does the `crontab` task automatically restart AnQiCMS after the process terminates unexpectedly?

Calendar 👁️ 63

As an expert who is well-versed in the operation of AnQiCMS, I know the importance of stable system operation for content management.AnQiCMS is an enterprise-level content management system developed based on the Go language, and its high performance and reliability are one of its core advantages.Even though the system itself is robustly designed, the complex and changing server environment may still result in unexpected termination of processes.In this case, how to ensure that AnQiCMS can quickly recover the service is a key point that website operators must pay attention to.AnQiCMS combines the Linux system'scrontabThe task plan has implemented an automatic restart mechanism after the process is unexpectedly terminated.

AnQiCMS process monitoring and automatic recovery mechanism

The auto-launch mechanism of AnQiCMS mainly relies on the Linux/Unix system incrontabService, this is a tool used for scheduling and executing periodic tasks.The core idea is to regularly check the running status of the AnQiCMS main process, and if it is detected that the process is not running, then automatically execute the start command to pull it up.This ensures the continuous availability of AnQiCMS services and greatly reduces the manual intervention burden on operations personnel.

crontabTask scheduling configuration

In the AnQiCMS deployment document, it explicitly guides users to configure one when deploying on a Linux servercrontabSchedule task. This scheduled task will execute a script namedstart.sheach minute. A typicalcrontabentry 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 sample path of the script, it 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 to implement the automatic launch logic. It does not start AnQiCMS unconditionally, but first checks if the main process of AnQiCMS is running, and only starts the operation when the process does not exist.This effectively avoids the problem of repeated startup during the normal operation of AnQiCMS, which leads to multiple instance operation or resource waste.

The simplified interpretation 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 the script are as follows:

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

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

  • ps -efList all running processes.
  • grep '\<anqicms\>'contains the keyword "anqicms", among the lines where\<and\>Is a word boundary, make sure to match the complete process name "anqicms" and not other strings containing "anqicms".
  • grep -v grepexcludegrepThe process line generated by the command itself, to 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 is not running, then execute the start command:cd $BINPATH && nohup $BINPATH/$BINNAME >> $BINPATH/running.log 2>&1 &.

  • cd $BINPATH: Switch to the AnQiCMS installation directory, which is crucial for ensuring that the program can correctly find its dependency files.
  • nohup $BINPATH/$BINNAME: Use.nohupCommand to start the AnQiCMS executable file.nohupThe purpose is to allow the program to continue running after the user exits the terminal, preventing process termination due to an SSH session disconnect.
  • >> $BINPATH/running.log 2>&1Redirect the standard output and standard error of the program torunning.logIn the 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 collaboration of scripts, AnQiCMS has implemented a simple and efficient process guardian mechanism. Once the AnQiCMS process is unexpectedly terminated due to any reason (such as program crash, memory overflow, etc.),crontabIt will detect this situation within the maximum of one minute and execute immediatelystart.shscript to automatically launch AnQiCMS and restore the website service.

The advantage of this mechanism lies in:

  • High availability:Significantly reduced service interruption time, improved the website's continuous online capability.
  • Deployment is simple:Configuration with just a few simple commandscrontaband scripts, without the need for complex process management tools.
  • Resource efficient:Only start processes when necessary, avoiding unnecessary resource consumption.
  • Easy to maintain:The script logic is clear, 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 operators to manage content securely and focus on business expansion.


Frequently Asked Questions (FAQ)

1. Ifstart.shThe script itself failed to execute, will AnQiCMS still be launched?

Ifstart.shThe internal logic of the script is incorrect (for example,BINPATHorBINNAMEIncorrect settings, or permission issues that prevent the execution of the startup command will result in AnQiCMS not being started correctly.crontabIt will continue to execute the script every minute, but it will fail each time due to an internal error in the script. You can checkrunning.logorcrontabthe logs (usually in/var/log/syslogor/var/log/cronto investigatestart.shThe execution status and error information of the script.

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

To manually stop AnQiCMS and preventcrontabImmediately pull it up, you need to edit it firstcrontabConfigure, comment out or delete the AnQiCMS startup task. 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 directorystop.shscript or manually find andkillterminate the AnQiCMS process usingps -ef | grep anqicmsFind the process ID, thenkill -9 <PID>Cancel after completing maintenance or debuggingcrontabComment for the task

3.nohupand&Instart.shWhat is the importance of the script?

nohupand&It is the key to ensure that AnQiCMS runs stably as a background service.

  • nohupIts function is to ignore the HUP (hang-up) signal. This means that even if you close the terminal session that started AnQiCMS (for example, if the SSH connection is disconnected), the AnQiCMS process will not be terminated and will continue to run in the background.
  • &This symbol puts the command into the background. 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 process for a long time without user sessions, which is a standard and necessary configuration for any web service.

Related articles

The `lsof -i:{port number}` command is used for port conflict troubleshooting in AnQiCMS, what is its specific function?

As an experienced CMS website operation person in an information security company, I am very clear about the various technical challenges I encounter in daily maintenance, especially those related to server environment configuration and system stable operation.Among them, port conflict is a common problem that also directly affects the availability of services.When your AnQiCMS website suddenly becomes inaccessible, or you encounter obstacles when deploying a new site, the `lsof -i:{port number}` command often becomes the key to quickly locate and solve the problem.

2025-11-06

When AnQiCMS default port is occupied, how to find and terminate the conflicting process through PID?

HelloAs a senior security CMS website operator, I fully understand the difficulties encountered in the operation when technical problems arise, especially when it affects the normal operation of the website.Port occupancy is one of the common but relatively easy problems to solve.When the default running port of AnQi CMS is occupied by other programs, the system will fail to start normally.Below, I will explain in detail how to locate and terminate these conflicting processes to ensure your safe CMS website goes live.

2025-11-06

How to verify that the `grep '\u003canqicms>'` in the `AnQiCMS` startup script can accurately match the process name?

As an experienced senior person proficient in Anqing CMS operation, I am well aware of the importance of system stability and the rigor of process management for the continuous operation of the website.Each line of command in the startup script carries the responsibility of ensuring the health of the service, especially the critical instructions for process detection.Next, I will elaborate on how to verify the accuracy of the `grep '<anqicms>'` command in the `AnQiCMS` startup script, ensuring that it can accurately match the running process of AnQiCMS.### Understanding the Core Mechanism of Process Detection In the Linux Environment

2025-11-06

What is the role of `nohup` and `&` in the background running of the process in the AnQiCMS startup script?

As a website operator who has been deeply involved in the CMS of security enterprises for many years, I am well aware of the importance of every detail for the stable operation of the website, especially on the server side, process management is of great importance.Today, let's delve into the crucial roles played by the seemingly simple symbols `nohup` and `&` in the startup script of AnQiCMS, and how they ensure our website remains continuously online.###

2025-11-06

If the `BINPATH` setting in the AnQiCMS startup script is incorrect, what PID-related issues may arise?

As an experienced AnQiCMS website operator, I know that every detail of the startup script may affect the stable operation of the system.In the daily operation and maintenance of AnQiCMS, the `BINPATH` variable in the startup script plays a vital role, indicating the storage path of the AnQiCMS executable files.Once this path is set incorrectly, a series of issues related to the Process ID (PID) will arise, directly affecting the startup, monitoring, and management of the AnQiCMS service.

2025-11-06

How to modify the `start.sh` script so that it can still work normally after the AnQiCMS process name changes?

As a senior Anqi CMS website operation personnel, I am well aware of the importance of maintaining the stable operation of the system, especially in a changing application environment.AnQi CMS is favored by our operations team for its efficiency in Go language and ease of deployment.However, in daily operations, especially when it involves multi-site deployment or personalized configuration, one may encounter a situation where it is necessary to adjust the core startup script to adapt to the new process naming rules.### Understand the core function of the `start.sh` script `start.sh`

2025-11-06

AnQiCMS is deployed on Baota Panel, what is the difference between PID monitoring of Go projects and manual scripts?

As an experienced security CMS website operator, I know that the stable operation of the website is the foundation for the efficient publication and optimization of content.The choice of deployment and process management methods for systems like AnQiCMS, which are developed based on the Go language, directly affects the reliability and operation and maintenance efficiency of the system.Today, let's delve into the similarities and differences between the PID monitoring mechanism of the AnQiCMS Go project and the manual maintenance script when deployed on Baota panel.

2025-11-06

When encountering AnQiCMS process zombie (Zombie Process), can the PID management script solve it?

AnQiCMS is an enterprise-level content management system developed based on the Go language, which has won the favor of many small and medium-sized enterprises and content operation teams with its high efficiency, customizable and scalable features.As an experienced website operator, I am well aware of the importance of stable server operation for content platforms.In daily operation and maintenance, we occasionally encounter some abnormal server process situations, among which a "zombie process" is one of them.

2025-11-06