As a long-term professional responsible for the operation of AnQiCMS website, I am well aware that stable and efficient system operation is crucial for content publishing and user experience.The AnQi CMS has won the favor of users with its high-performance and concise architecture developed in Go language.In deployment and daily operation and maintenance, we often encounter a problem: how to ensure the continuous and stable operation of the AnQiCMS application, especially when facing unexpected crashes or server restarts.

AnQiCMSstart.shThe operation mechanism and limitations of the script

The deployment guide of AnQi CMS provides a script namedstart.shscript, whose core function is to check the AnQi CMS main program (usually}anqicmsIs the binary file running. If the program is not running, the script will proceed throughnohup /path/to/anqicms >> /path/to/running.log 2>&1 &The command starts AnQiCMS in the background and redirects the output to a log file. To achieve the automatic monitoring of the process, we usually configure thestart.shscript tocrontabIn order to execute it once a minute, it is used to detect and restart the AnQiCMS process that may have stopped.

This is based oncrontabandstart.shThe guard mode is simple and easy to use, which is enough to deal with small sites or development environments, but it has obvious limitations in production environments. First,start.shOnly by checking if the process ID exists can we determine if the program is running. It cannot recognize the situation where the program exists but is in a 'zombie' state, unresponsive, or internally crashed. Secondly,crontabThe minimum execution cycle is usually one minute, which means that once AnQiCMS fails, the system needs to wait up to one minute to detect and attempt to restart, during which the service interruption will have a negative impact on user experience and SEO. In addition,start.shLack of log rotation, resource monitoring, dependency service checking, and other advanced features makes fault troubleshooting and system maintenance complex.

The necessity of introducing advanced process monitoring tools

To enhance the availability, stability, and manageability of AnQiCMS in the production environment, simply rely onstart.shThe script is far from enough.We need to introduce a more professional and intelligent process monitoring tool.These tools can monitor the health of applications in real time, not just the existence of the process; they can automatically restart the program immediately when it crashes, significantly shortening the service downtime; at the same time, they also provide rich configuration options, supporting resource limitations, log management, startup sequence of dependent services, web management interface, and other functions, greatly simplifying the operation and maintenance work.

For example,MonitandSupervisorThese are two widely popular process monitoring tools, each with its own features and advantages, but they can all effectively replacecrontabandstart.shThe combination provides AnQiCMS with stronger guardian capabilities.

Integration with Supervisor

SupervisorIt is a client/server system written in Python that allows users to monitor and control a large number of processes on Unix-like operating systems.It is a lightweight, feature-rich process manager, very suitable for managing single-instance applications like AnQiCMS.

To integrate AnQiCMS withSupervisorIntegration, first make sure that the server on which you are installing is installedSupervisor. Then, you can define the service of AnQiCMS by creating a new configuration file.

The following is the typical configuration steps for integrating AnQiCMS toSupervisor:

  1. Stop and remove the existingcrontabtask:This is the critical first step. You need to editcrontab(crontab -eDelete all related withstart.shandstop.shscheduling tasks to avoid conflicts.

  2. Create a Supervisor configuration file:In/etc/supervisor/conf.d/in the directory (or youSupervisorConfigure a new location (specified) to create.confa file, for exampleanqicms.conf. The content of the file is roughly as follows:

    [program:anqicms]
    command=/path/to/anqicms          ; AnQiCMS二进制文件的完整路径
    directory=/path/to/anqicms_root   ; AnQiCMS项目的根目录,包含config.json
    autostart=true                    ; Supervisor启动时自动启动AnQiCMS
    autorestart=true                  ; AnQiCMS异常退出时自动重启
    startretries=3                    ; 启动失败后尝试重启的次数
    user=www                          ; 运行AnQiCMS的用户,建议使用非root用户
    stopsignal=QUIT                   ; 发送QUIT信号给Go程序进行优雅关停
    stdout_logfile=/var/log/anqicms/anqicms_stdout.log ; 标准输出日志路径
    stderr_logfile=/var/log/anqicms/anqicms_stderr.log ; 错误输出日志路径
    stdout_logfile_maxbytes=10MB      ; 日志文件最大大小
    stdout_logfile_backups=5          ; 日志文件备份数量
    ; environment=ANQICMS_PORT="8001" ; 如果需要通过环境变量配置端口,可在此处添加
    

    Be sure to set/path/to/anqicmsand/path/to/anqicms_rootReplace with the actual installation path of AnQiCMS. For multi-site deployment, if each site runs on an independent AnQiCMS binary instance, you need to create an independent instance for each site.[program:...]Configure blocks and ensure they use different ports and log files.

  3. Update Supervisor configuration:Notify after saving the configuration file.SupervisorReload the configuration and start AnQiCMS:

    sudo supervisorctl reread
    sudo supervisorctl update
    sudo supervisorctl start anqicms
    

BySupervisorAnQiCMS will receive continuous process protection, detailed logging, and an easy-to-use management interface.

Integration with Monit

MonitIt is a small but powerful tool that can not only monitor processes, but also monitor files, directories, file systems, network connections, system resources (CPU, memory), and more.MonitCheck the status of the monitored item by polling and execute predefined actions (such as restarting the service, sending an alert) when an anomaly is detected.

The following is the typical configuration steps for integrating AnQiCMS toMonit:

  1. Stop and remove the existingcrontabtask:withSupervisorSimilarly, first you need to delete all related tostart.shandstop.shthe relatedcrontabTask.

  2. Create a Monit configuration file:In/etc/monit/conf.d/in the directory (or youMonitCreate a new configuration file at the specified location, for exampleanqicms.conf. The content of the file is roughly as follows:

    check process anqicms with pidfile /var/run/anqicms.pid
        start program = "/bin/bash -c 'cd /path/to/anqicms_root && nohup /path/to/anqicms > /var/log/anqicms/anqicms.log 2>&1 & echo $! > /var/run/anqicms.pid'" as uid www gid www
        stop program  = "/bin/kill `cat /var/run/anqicms.pid`"
        if failed host 127.0.0.1 port 8001 protocol http request "/" then restart
        if 5 restarts within 5 cycles then timeout
        if cpu > 80% for 5 cycles then restart
        if mem > 200 MB for 5 cycles then alert
        group anqicms_apps
        depends on mysql_db ; 假设AnQiCMS依赖MySQL,这里可以添加对数据库的依赖检查
    

    Please note:

    • /path/to/anqicmsand/path/to/anqicms_rootReplace it with the actual path.
    • pidfileThe path needs to ensure that the AnQiCMS startup script can write the PID to this file. Since the Go program does not generate PID files by default, you may need tostart programManually modify the startup command, for exampleecho $! > /var/run/anqicms.pidTo record the PID.
    • stop programYou can usekillCommand, or write a more elegantstop.shScript to sendSIGTERMSignal, give the program a chance to clean up resources before exiting.
    • if failed host ...: This is a health check,Monitwill attempt to access the AnQiCMS HTTP port, and if it fails, it will restart. Port8001Should be consistent with AnQiCMSconfig.jsonThe port configured in
    • depends on mysql_db: This is an example of a dependency, indicating that AnQiCMS can only start normally when the MySQL database service is available.
  3. Update Monit configuration:Check after saving the configuration file,Monitreload the syntax of the configuration:

    sudo monit -t       ; 检查语法
    sudo systemctl reload monit ; 重新加载Monit配置
    

    MonitThe monitoring of the AnQiCMS process will begin, and appropriate actions will be taken when the configured conditions are triggered.

Advantages brought by integration

AnQiCMS will be integrated withSupervisororMonitIntegrate advanced process monitoring tools, which can bring significant advantages in many aspects of your website operation:

  • High availability:The application can automatically restart quickly when it crashes, minimizing downtime to ensure the continuous online operation of AnQiCMS.
  • Resource management:Can monitor the CPU, memory and other resource usage, and execute preset actions (such as restart) when resources are abnormal, to prevent resource exhaustion from causing system crashes.
  • Health check:Supports in-depth health checks for applications, such as checking if the service responds to HTTP requests, not just whether the process is alive.
  • Log management:Provide automated log file rotation and management to prevent log files from growing indefinitely and consuming disk space, and to facilitate log analysis.
  • Centralized management:Provide a unified command-line or Web interface to manage all monitored AnQiCMS instances and other services, improving O&M efficiency.
  • Alarm notification:When AnQiCMS encounters problems, it can be configured to send email, SMS, or other notification methods in a timely manner to alert, allowing operations personnel to know and intervene in the first time.

By this integration, we can free ourselves from the tedious manual checks and restarts, focusing on content creation and website optimization, allowing AnQiCMS to run continuously in a more robust and reliable environment.


Frequently Asked Questions (FAQ)

**1. Why not