For AnQiCMS `start.sh`, how to implement more complex conditional startup logic in `crontab`?

Calendar 👁️ 56

As an experienced website operations expert, I know that the core of a stable and efficient website system lies in automation and intelligence.AnQiCMS with its excellent performance and simple architecture has become the preferred choice for many webmasters.However, even the most powerful system cannot do without careful operation and maintenance.start.shScript, incrontabImplement more complex conditional startup logic to make our website start more intelligently and reliably.

Introduction: Automation operations, starting from an elegant startup.

AnQiCMS's deployment usually involves using astart.shscript to start the core service and cooperate withcrontabImplement timing check and guard.This pattern is simple and efficient, but it also has certain limitations.When the server environment is complex and variable, or AnQiCMS depends on other services (such as databases, caches), simply starting AnQiCMS by checking if the process exists is not robust enough.We hope not only that AnQiCMS can be 'booted automatically' or 'rebooted when crashed', but also that it can check the surrounding environment carefully before startup, ensuring everything is ready before going online calmly.

understanding the existing startup mechanism of AnQiCMS

In the official documentation of AnQiCMS, for examplestart.mdandinstall.mdWe can see a typicalstart.shscript example:

#!/bin/bash
### check and start AnqiCMS
# author fesion
# the bin name is anqicms
BINNAME=anqicms
BINPATH=/www/wwwroot/anqicms # 需要根据实际路径修改

# 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

The core logic of this script is very clear: it checks if a process namedanqicmsexists in the system. If it does not exist(exists -eq 0It switches to the installation directory of AnQiCMS and then usesnohupcommand to start in the backgroundanqicmsexecutable file and redirects the standard output and error output torunning.log.

AndcrontabThis is usually configured to run every minutestart.shScript:*/1 * * * * /www/wwwroot/anqicms/start.sh

This mechanism is simple and efficient, but it is also prone to problems in the following scenarios:

  • The dependent service is not ready: When AnQiCMS starts, if the MySQL database it depends on has not yet fully started or is in an abnormal state, AnQiCMS may fail to start or crash immediately after starting.
  • Insufficient resources: The server disk space is insufficient or the memory is too low, even if AnQiCMS is started, it cannot run normally.
  • Environment exception:Certain key configuration files are missing or permissions are incorrect, causing AnQiCMS to fail to load.

In response to these issues, we need to upgrade thestart.shscript to an 'intelligent upgrade'.

Build smarterstart.shAdd conditional judgment logic

The core concept is to carry out a series of "health checks" on the operating environment before AnQiCMS truly starts up.Only when all the items have passed the inspection, can AnQiCMS smoothly go online.

1. Check database availability

AnQiCMS is a content management system, the database is its lifeline.Before starting AnQiCMS, it is crucial to ensure that the database service is running normally.We can determine this by checking the database listening port.

# ... (脚本开头的定义不变) ...

# 定义数据库连接信息 (根据实际情况修改)
DB_HOST="127.0.0.1"
DB_PORT="3306"
# 如果使用unix socket,则需要检查socket文件
# DB_SOCKET="/var/run/mysqld/mysqld.sock" 

# --- 检查数据库服务是否可用 ---
echo "$(date +'%Y%m%d %H:%M:%S') INFO: Checking database availability..." >> $BINPATH/check.log
# 检查TCP端口 (适用于远程或本地TCP连接)
nc -z -w 1 $DB_HOST $DB_PORT
if [ $? -ne 0 ]; then
    echo "$(date +'%Y%m%d %H:%M:%S') ERROR: MySQL server at $DB_HOST:$DB_PORT is not reachable. AnQiCMS will not start." >> $BINPATH/check.log
    # 可选:发送通知邮件
    # echo "AnQiCMS startup failed: MySQL not reachable." | mail -s "AnQiCMS Alert" [email protected]
    exit 1 # 退出脚本,阻止 AnQiCMS 启动
fi
echo "$(date +'%Y%m%d %H:%M:%S') INFO: MySQL server is reachable." >> $BINPATH/check.log

# ... (AnQiCMS 的 PID 检查和启动逻辑) ...

We used herenc(netcat) tool to check if the port is open.-zThe option indicates to only scan listeners without sending any data;-w 1Indicates a timeout of 1 second.

2. System resource check

Sufficient disk space and memory are the foundation for stable system operation.

# ... (数据库检查后) ...

# --- 检查磁盘空间 ---
MIN_DISK_FREE_GB=5 # 最低要求空闲磁盘空间 (GB)
CURRENT_DISK_FREE_GB=$(df -h $BINPATH | awk 'NR==2 {print $4}' | sed 's/G//')
if (( $(echo "$CURRENT_DISK_FREE_GB < $MIN_DISK_FREE_GB" | bc -l) )); then
    echo "$(date +'%Y%m%d %H:%M:%S') ERROR: Disk space critically low ($CURRENT_DISK_FREE_GB GB available, $MIN_DISK_FREE_GB GB required). AnQiCMS will not start." >> $BINPATH/check.log
    # 可选:发送通知
    exit 1
fi
echo "$(date +'%Y%m%d %H:%M:%S') INFO: Sufficient disk space available ($CURRENT_DISK_FREE_GB GB)." >> $BINPATH/check.log

# --- 检查内存使用 (简单示例,可根据需求细化) ---
# 检查是否有足够的空闲内存,这里只是一个示例,实际生产可能需要更复杂的逻辑
FREE_MEM_KB=$(free -m | awk 'NR==2{print $4}') # 获取空闲内存 (MB)
MIN_FREE_MEM_MB=512 # 最低要求空闲内存 (MB)
if [ "$FREE_MEM_KB" -lt "$MIN_FREE_MEM_MB" ]; then
    echo "$(date +'%Y%m%d %H:%M:%S') ERROR: Memory critically low ($FREE_MEM_KB MB available, $MIN_FREE_MEM_MB MB required). AnQiCMS will not start." >> $BINPATH/check.log
    exit 1
fi
echo "$(date +'%Y%m%d %H:%M:%S') INFO: Sufficient memory available ($FREE_MEM_KB MB)." >> $BINPATH/check.log

# ... (AnQiCMS 的 PID 检查和启动逻辑) ...

We used heredf -hCheck disk space,awkandsedPerform data extraction and formatting.bc -lUsed for floating-point number comparison.free -mCheck memory.

3. Dependency service check: Nginx/Apache reverse proxy

If the AnQiCMS front-end has Nginx or Apache as a reverse proxy, it is also very important to ensure that the proxy service is running properly.

”`bash

… (resource check after) …

— Check Nginx/Apache process (modify according to the actual web server used) —

Example: Check Nginx

if ! pgrep -x "nginx" > /dev/null; then

echo "$(date +'%Y%m%d %H:%M:%S') WARN: Nginx is not running. AnQiCMS will start, but might not be accessible externally." >> $BINPATH/check.log
# 警告而非错误,因为AnQiCMS自身可以独立运行,只是外部访问有问题
# 如果Web服务器是AnQiCMS运行的强依赖,这里可以改为 exit 1

else

echo "$(date

Related articles

Which is more suitable for service self-startup during the AnQiCMS deployment, `crontab` or `systemd`?

As an experienced website operations expert, I know that the stable operation of a CMS system is the foundation for the continuous and efficient distribution of website content.AnQiCMS (AnQiCMS) is widely popular among small and medium-sized enterprises and content operation teams for its high-performance and scalable features based on the Go language.However, even the most excellent system cannot do without reliable infrastructure support, among which the service's self-starting strategy is of paramount importance.Today, let's delve into which is more suitable as the service startup solution, `crontab` or `systemd`, during the deployment process of AnQiCMS

2025-11-06

How to quickly verify the syntax of the AnQiCMS `start.sh` script under the `crontab -e` environment?

As an experienced website operations expert, I am well aware that ensuring the smooth operation of all automated tasks in the daily maintenance of AnQiCMS is crucial.Especially scripts like `start.sh`, which are critical startup scripts, are responsible for maintaining the stability of AnQiCMS core services.However, after adding the script to the `crontab -e` scheduled task, many operation personnel have encountered the problem that script syntax errors cause the task to fail silently and be difficult to detect.Today, let's delve into how to use the `crontab -e` environment

2025-11-06

Where should `crontab` be checked first if AnQiCMS frequently restarts or stops abnormally?

AnQiCMS is an efficient and lightweight content management system, and its stable operation is the foundation of website operation.However, during use, you may occasionally encounter situations where the system frequently restarts or stops abnormally, which undoubtedly poses a great challenge to the availability of the website.When such a problem arises, you may feel some anxiety, not knowing where to start.As the 'watchman' of the website, the `crontab` (schedule task) is often the first place we need to examine, because it is responsible for the continuous operation and monitoring of the AnQiCMS core process. AnQiCMS

2025-11-06

What is the role and modification scenario of the `BINNAME` variable in AnQiCMS `start.sh`?

As an experienced website operations expert, I know that every system configuration detail may affect the stability and operation efficiency of the website.Today, let's delve into a variable in AnQiCMS that seems trivial but plays a crucial role in actual deployment and multi-site management - the `BINNAME` in the `start.sh` script.Understand its role and modification scenarios, which can help us manage AnQiCMS sites more efficiently and safely.### Reveal the contents of AnQiCMS `start.sh`

2025-11-06

What is the diagnostic value of the log output `running.log` in the AnQiCMS `crontab` configuration?

AnQiCMS as an efficient and customizable enterprise-level content management system, its stable operation is the foundation of website operation.It is crucial to understand the internal operation mechanism and diagnostic tools during the deployment and maintenance of AnQiCMS.Among them, the `running.log` file generated by the `crontab` configuration is a highly diagnostic 'black box' that records the startup status and critical information of the system's core services in the early stages of operation.As an experienced website operation expert, I will take you deep into the analysis of `running.log`

2025-11-06

Why does AnQiCMS `start.sh` need to check PID to avoid repeated startup?

## Core Guardian: Deep Consideration of PID Check in AnQiCMS `start.sh` Script As an expert in website operations for many years, I am well aware that every detail of system stability is crucial.For a content management system like AnQiCMS that追求high efficiency and simplicity, even though its underlying Go language has excellent concurrent processing capabilities, we still need a rigorous mechanism to ensure the robustness of its operating environment.Today, let's delve deeply into something that seems simple

2025-11-06

AnQiCMS `crontab` task did not run on time, how to troubleshoot and correct it?

As an experienced website operations expert, I know that scheduled tasks (`crontab`) play a crucial role in content management systems, especially in platforms like AnQiCMS that pursue efficiency and automation.It not only ensures timely content release and regular data backup, but also provides automatic recovery protection in case of system anomalies.

2025-11-06

What is the relationship between configuring Nginx reverse proxy and `crontab` after manual deployment of AnQiCMS?

As an experienced website operations expert, I fully understand the importance of every link in the process of building and maintaining a website, especially for a system like AnQiCMS (AnQiCMS) that pursues efficiency and stability. The configuration of various items after deployment needs to be carefully crafted.Today, let's delve into the relationship between Nginx reverse proxy and `crontab` scheduled tasks after AnQiCMS manual deployment, and how they work together to ensure the stable operation of your website.## After manual deployment of AnQiCMS, configure Nginx

2025-11-06