How to manage the startup of different instances of `crontab` when deploying AnQiCMS across multiple sites?
AnQiCMS with its efficient and flexible architecture provides strong support for enterprise content management.Especially when dealing with multi-site requirements, AnQiCMS provides various deployment solutions, one of which is to run multiple independent AnQiCMS instances on the same server to manage different websites.How to cleverly utilize when adopting this deployment modecrontabSchedule tasks to ensure the stable startup and continuous operation of these different instances have become a key link that website operators need to understand in depth.
This article will focus on this topic, elaborating on the AnQiCMS multi-instance deployment undercrontabmanagement strategy, aimed at helping you build a robust, high-availability multi-site environment.
Understand the multi-site deployment mode of AnQiCMS
There are usually two main ways of multi-site deployment in AnQiCMS. One isSingle AnQiCMS instance manages multiple websitesYou can configure multiple domains in an AnQiCMS backend, and use reverse proxy to direct different domains to the same AnQiCMS service port (such as 8001). The system internally resolves the domain to different website content. In this mode, there is only one AnQiCMS process running, socrontabPay attention only to the startup and maintenance of this process.
However, what we are focusing on today is another deployment method with more isolation:Run multiple independent AnQiCMS instances on a serverThis means that each AnQiCMS instance is an independent Go language executable file and its accompanying data and configuration, each listening on a different port and managing one or more websites.This pattern provides higher isolation and independence, but it also brings the need for multi-process management, especially after server restarts or unexpected process termination, how to ensure that each instance can be automatically started up iscrontabWhere it plays a role.
Of a single AnQiCMS instancecrontabStartup mechanism
AnQiCMS provided a simple and efficientstart.shThe script to manage the startup of a single instance. The core logic of this script is to check if the AnQiCMS process is already running, and if not, start it.This is usually achieved by checking the name of the specific executable file in the process list.
A typicalstart.shThe script content may look like this:
#!/bin/bash
### check and start AnqiCMS
# author fesion
# the bin name is anqicms
BINNAME=anqicms
BINPATH=/www/wwwroot/anqicms_site1 # 假设这是实例1的路径
# check the pid if exists
exists=`ps -ef | grep '\<anqicms\>' |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
In order to run this script automatically in the system background and regularly check the AnQiCMS process, we will configure it tocrontabChinese. For example, set it to execute once every minute to ensure process stability:
# 编辑 crontab
crontab -e
# 在打开的编辑器中添加一行,指向您的 start.sh 脚本路径
*/1 * * * * /www/wwwroot/anqicms_site1/start.sh
Here/www/wwwroot/anqicms_site1is the deployment directory of your first AnQiCMS instance.
Manage expansion to multiple AnQiCMS instances
When you need to run multiple independent AnQiCMS instances on the same server, the core principle of management strategy isProvide a set of independent and distinguishable startup and monitoring mechanisms for each instance. The design of AnQiCMS allows you to achieve this by copying its program code.
Copy instance code and configurationFirstly, you need to create a complete code copy for each independent AnQiCMS instance. For example, if you have
anqicms_site1instance, now you need to addanqicms_site2Then you should create a similar directory on the server/www/wwwroot/anqicms_site2and copy all AnQiCMS files (including executable files,config.jsonetc.) to the new directory.Configure a separate portEach AnQiCMS instance must listen to a unique port. Find the file in the root directory of each instance,
config.jsonand modify the following in the file,portThe field has different values. For example,anqicms_site1Use8001,anqicms_site2then it is modified to8002,anqicms_site3Use8003and so on.Rename the executable file (recommended practice)for convenience
crontabScript and system monitoring can clearly distinguish between different AnQiCMS processes, it is strongly recommended that you rename the AnQiCMS executable files in each instance directory. For example, change/www/wwwroot/anqicms_site1/anqicmsRenamed toanqicms_site1_bin, will/www/wwwroot/anqicms_site2/anqicmsRenamed toanqicms_site2_binThe benefit of doing this is that when you executeps -ef | grep anqicmsthe command, you can clearly see the process information of each instance and avoidgrepmismatching with other related processes.Create an independent one for each instance
start.shscriptCreate customized for each AnQiCMS instance based on the renamed executable file and their respective installation pathsstart.shscript. The logic of these scripts is similar to that of a single instance, butBINNAMEandBINPATHthe variables will point to the unique information of each instance.For example,
anqicms_site2ofstart.shscript