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 startupsIf
start.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:
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 findanqicmsexecutable 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_site2modify
config.jsonConfiguration: Ensure that each instance'sconfig.jsonfile has a unique port number. For example,site1Use8001,site2Use8002.// /www/wwwroot/site1/config.json { "port": 8001, // ...其他配置 } // /www/wwwroot/site2/config.json { "port": 8002, // ...其他配置 }Create an independent one for each instance
start.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 & fistop.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" fiImportant reminder: Please make sure to
BINPATHChange 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:
modify
start.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.logmodify
stop.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/bashstop 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')