How to avoid PID conflicts between different instances when deploying AnQiCMS across multiple sites?

Calendar 👁️ 60

As an experienced security CMS operator, I know that system stability and efficiency are crucial when managing multiple websites.AnQi CMS takes advantage of the high concurrency features of the Go language and the multi-site management function, providing us with strong support.However, when deploying multiple secure CMS instances on the same server, how to properly handle process ID (PID) conflicts to ensure that each instance runs independently and stably is a core issue that needs to be paid attention to in operation and maintenance.

Understand the PID conflict in multi-site deployment

The 'Multi-site Management' feature of Anqi CMS allows us to manage multiple independent websites under the same system architecture.This usually means that each website instance needs a separate running process.anqicms) Start. When we start multiple secure CMS instances on the server, each instance will try to run this binary file.

Provided by AnQi CMSstart.shA script whose core function is to check if the process namedanqicmsis running, if not, it will start it. The key part of the script usesps -ef | grep '\<anqicms\>'Command to find the process. In a single instance deployment, this is not a problem. But when multiple instances run on the same server, if all instances use the same binary filenameanqicmsand are all independentstart.shscripts to manage, then allstart.shscripts will all see all namedanqicmsprocess.

This will lead to the following problems:

  • Duplicate startupsIfstart.shDetectedanqicmsThe process exists, even if it is a process of another instance, it may also think that its own instance is already running, and thus stop starting.On the contrary, if the detection mechanism is not accurate enough, it may cause multiple scripts to attempt to start the same instance, or to mistake other instances for themselves.
  • Error stop:stop.shThe script usually goes throughkillcommand combinationgrepLooking for the process ID. If it is looking for a generalanqicmsProcess name, then executekill -9 $existsIt may incorrectly terminate all running Anqicms instances, not just the target instance. This "massacre" is catastrophic for a multi-site environment.

To avoid this potential risk, we need to provide a unique identifier for each safe CMS instance so that their management scripts can accurately control their respective processes.

Core strategy: Assign a unique identity to each instance

The key to resolving PID conflicts is to ensure that each Anqi CMS instance has a unique identifier at the system level.The official documents and community practices of AnQi CMS mainly recommend two strategies to achieve this goal.

I. Distinguish instances through unique binary file names

This is the most intuitive method recommended in the official document of AnQi CMS.Assign a unique name to the binary executable file for each instance.site1.comandsite2.com, You can rename theiranqicmsBinary files separately renaminganqicms_site1andanqicms_site2.

The modification steps are as follows:

  1. Copy the AnQi CMS code and rename the binary file: Copy a complete AnQi CMS installation package for each new site to a separate directory (for example/www/wwwroot/site1and/www/wwwroot/site2). Go into the root directory of each site and findanqicms executable file and rename it. For example:

    
    mv /www/wwwroot/site1/anqicms /www/wwwroot/site1/anqicms_site1
    mv /www/wwwroot/site2/anqicms /www/wwwroot/site2/anqicms_site2
    

  2. modifyconfig.jsonConfiguration: Ensure that each instance'sconfig.json file has a unique port number. For example,site1Use8001,site2Use8002.

    
    // /www/wwwroot/site1/config.json
    {
      "port": 8001,
      // ...其他配置
    }
    
    // /www/wwwroot/site2/config.json
    {
      "port": 8002,
      // ...其他配置
    }
    

  3. Create an independent one for each instancestart.shandstop.shscript: Each instance should have its ownstart.shandstop.shscripts located in their respective root directories. These scripts need to be updatedBINNAMEandBINPATHVariable to match the new binary filename and path.

    start.shExample (for)anqicms_site1Instance):

    #!/bin/bash
    ### check and start AnqiCMS for site1
    # author fesion
    # the bin name is anqicms_site1
    BINNAME=anqicms_site1
    BINPATH=/www/wwwroot/site1 # 更改为当前实例的实际路径
    
    # check the pid if exists using the unique binary name
    exists=`ps -ef | grep '\<${BINNAME}\>' |grep -v grep |wc -l`
    echo "$(date +'%Y%m%d %H:%M:%S') ${BINNAME} PID check: $exists" >> ${BINPATH}/check.log
    echo "PID ${BINNAME} check: $exists"
    if [ $exists -eq 0 ]; then
        echo "${BINNAME} NOT running"
        cd ${BINPATH} && nohup ${BINPATH}/${BINNAME} >> ${BINPATH}/running.log 2>&1 &
    fi
    

    stop.shExample (for)anqicms_site1Instance):

    #!/bin/bash
    ### stop anqicms for site1
    # author fesion
    # the bin name is anqicms_site1
    BINNAME=anqicms_site1
    BINPATH=/www/wwwroot/site1 # 更改为当前实例的实际路径
    
    # check the pid if exists using the unique binary name
    exists=`ps -ef | grep '\<${BINNAME}\>' |grep -v grep |awk '{printf $2}'`
    echo "$(date +'%Y%m%d %H:%M:%S') ${BINNAME} PID check: $exists" >> ${BINPATH}/check.log
    echo "PID ${BINNAME} check: $exists"
    if [ $exists -eq 0 ]; then
        echo "${BINNAME} NOT running"
    else
        echo "${BINNAME} is running"
        kill -9 $exists
        echo "${BINNAME} is stop"
    fi
    

    Important reminder: Please make sure toBINPATHChange the variable to the actual deployment path of the corresponding instance and ensureBINNAMEmatches the binary file name you renamed.

Second, manage processes through PID files (a more robust operation and maintenance practice)

Although the method of renaming binary files mentioned above can effectively solve most PID conflict problems, relying on PID files to manage processes is a more robust and standardized practice in complex O&M environments. The PID file stores the unique PID of a specific process, allowing scripts to accurately start, stop, and monitor individual processes, and avoidsgrepCommand misjudgment may occur (for example, if an unrelated program happens to containanqicmsas part of its command line arguments).

The modification steps are as follows:

  1. modifystart.shThe script is used to create and use PID files: When starting the AnQiCMS instance, itsstart.shthe script should write the current process's PID to a specific PID file, such asanqicms.pid)

    #!/bin/bash
    ### check and start AnqiCMS with PID file
    BINNAME=anqicms # 保持二进制文件名不变,或者使用唯一的名称
    BINPATH=/www/wwwroot/site1 # 更改为当前实例的实际路径
    PIDFILE=${BINPATH}/anqicms.pid # 定义PID文件路径
    
    # 检查PID文件是否存在并且PID是否活跃
    if [ -f "$PIDFILE" ]; then
        PID=$(cat "$PIDFILE")
        if ps -p "$PID" > /dev/null; then
            echo "$(date +'%Y%m%d %H:%M:%S') ${BINNAME} already running with PID ${PID}" >> ${BINPATH}/check.log
            exit 0
        else
            echo "$(date +'%Y%m%d %H:%M:%S') PID file exists but process not running. Cleaning up stale PID file." >> ${BINPATH}/check.log
            rm -f "$PIDFILE"
        fi
    fi
    
    echo "$(date +'%Y%m%d %H:%M:%S') Starting ${BINNAME}..." >> ${BINPATH}/check.log
    cd ${BINPATH}
    nohup ${BINPATH}/${BINNAME} >> ${BINPATH}/running.log 2>&1 &
    echo $! > "$PIDFILE" # 将新启动的进程PID写入PID文件
    echo "$(date +'%Y%m%d %H:%M:%S') ${BINNAME} started with PID $(cat "$PIDFILE")" >> ${BINPATH}/check.log
    
  2. modifystop.shThe script reads the PID file and terminates the process:stop.shThe script will read the PID from the corresponding PID file and only terminate the process with that PID. “`bash #!/bin/bash

    stop AnqiCMS with PID file

    BINNAME=anqicms # Keep the binary filename unchanged or use a unique name BINPATH=/www/wwwroot/site1 # Change to the actual path of the current instance PIDFILE=${BINPATH}/anqicms.pid # Define PID file path

    if [ -f “$PIDFILE” ]; then

    PID=$(cat "$PIDFILE")
    if ps -p "$PID" > /dev/null; then
        echo "$(date +'%Y%m%d %H:%M:%S') Stopping ${BINNAME} with PID ${PID}..." >> ${BINPATH}/check.log
        kill "$PID" # 发送TERM信号
        sleep 5 # 等待进程优雅关闭
        if ps -p "$PID" > /dev/null; then
            echo "$(date +'%Y%m%d %H:%M:%S') ${BINNAME} (PID ${PID}) did not terminate gracefully. Forcing kill." >> ${BINPATH}/check.log
            kill -9 "$PID" # 强制终止
        fi
        rm -f "$PIDFILE" # 移除PID文件
        echo "$(date +'%Y%m%d %H:%M:%S') ${BINNAME} stopped." >> ${BINPATH}/check.log
    else
        echo "$(date +'%Y%m%d %H:%M:%S')
    

Related articles

How to adjust the `start.sh` script after changing the executable file name of AnQiCMS in order to continue working?

As an experienced CMS website operation personnel of Anqi, I am well aware of the importance of maintaining the stable operation of the website, especially when adjusting the system's core files.AnQiCMS with its efficiency and lightweight nature in the Go language is widely popular in actual operation.However, any modification to the core components requires meticulous adjustments to ensure the normal operation of the system.Among them, after changing the executable file name of AnQiCMS, the corresponding startup script `start.sh` must also be adjusted for compatibility.

2025-11-06

What impact does the `BINNAME` variable have on process identification in the `start.sh` script?

HelloAs an experienced CMS website operation personnel, I fully understand how important it is to have a clear grasp of the underlying mechanisms when managing the system.`start.sh` script's `BINNAME` variable seems simple, but it plays a core role in the process identification and stable operation of the security CMS.Below, I will elaborate on the impact of this variable on process identification.

2025-11-06

How does the mechanism of detecting PID with `grep '\u003canqicms>'` in `start.sh` work?

As an experienced CMS website operation personnel, I know that the stable operation of the website is the basis for the effective dissemination of content.In daily work, we must not only pay attention to the quality of the content and the user experience, but also ensure the continuous and reliable operation of the backend services.AnQi CMS provides a simple and efficient mechanism for this, using specific commands in the `start.sh` script to detect and maintain the running status of the core application.The `start.sh` script plays the role of a guardian in the deployment of AnQi CMS.Its main responsibility is to check if the main program is running

2025-11-06

If the AnQiCMS process is unexpectedly closed, how long will the `start.sh` script attempt to restart it?

As an experienced CMS website operation person in the security industry, I fully understand the importance of system stability for business continuity.AnQiCMS is a high-efficiency content management system, the stable operation of which is one of the key points of our daily maintenance.When the AnQiCMS main process is closed due to an abnormal condition, the system is designed to ensure that the service can recover quickly through automated scripts.

2025-11-06

How to configure independent `start.sh` and `stop.sh` scripts for each AnQiCMS multi-site instance?

As an expert deeply familiar with AnQiCMS (AnQiCMS) operation, I understand that you want to implement independent management for each AnQiCMS site instance. In multi-site operations, although AnQiCMS provides powerful built-in multi-site management features, sometimes in order to achieve finer-grained resource isolation, independent process management, or personalized optimization for specific sites, we may need to run each site on an independent AnQiCMS application instance.

2025-11-06

What is the specific function and advantage of the `nohup` command in the AnQiCMS startup script?

In the daily operation of AnQiCMS, system stability and reliability are the core.As a high-performance content management system built with Go language, AnQiCMS needs to ensure that its core services can run continuously and without interruption.In the Linux server environment, the `nohup` command plays a crucial role in the startup script of AnQiCMS, ensuring this and providing many conveniences.

2025-11-06

Which AnQiCMS process information are recorded in the `running.log` and `check.log` files, and how can they be used for troubleshooting?

As an experienced CMS website operation personnel, I am well aware of the importance of website stability for content publication and user experience.In daily work, log files are an indispensable tool for diagnosing system health and quickly locating problems.AnQi CMS provides the `running.log` and `check.log` log files, which record different process information and together provide valuable clues for troubleshooting system failures.

2025-11-06

How to quickly judge whether the AnQiCMS process is running normally through the `check.log` file?

As a senior person who has been deeply engaged in the CMS content operation of AnQi for many years, I am well aware of the importance of website stability to the business.AnQi CMS, with its high efficiency, lightweight nature, and the high concurrency characteristics brought by the Go language, provides a solid foundation for our content management.However, any system may encounter unexpected situations, at this time, it is particularly important to quickly judge whether the core process is running normally.Today, I will explain in detail how to use the `check.log` file, this "heart monitor" built into AnQiCMS, to quickly diagnose the system status.

2025-11-06