How to write a AnQiCMS monitoring script that not only checks PID but also detects if the port is listening normally?

Calendar 👁️ 73

As a senior AnQiCMS website operations personnel, I am well aware of the importance of maintaining system stability for content distribution and user experience.We developed AnQiCMS based on the Go language, which is known for its high performance and stability, but in complex production environments, any system may encounter unexpected problems.Therefore, a reliable monitoring mechanism is essential. Today, I will introduce in detail how to write an efficient AnQiCMS monitoring script, which can not only check if the process (PID) is active, but also confirm that the AnQiCMS service port is normally listening, ensuring that your website is always online.

Why is it necessary to monitor processes and ports simultaneously?

Merely checking if the AnQiCMS process (PID) exists is not enough to completely confirm the health status of the service.Although the process exists means that the program may be running, it may have already entered a deadlock state, or it may not have bound to the expected port correctly due to internal errors, causing external requests to be inaccessible.

While monitoring whether the port is normally listening, it can be verified from the network level that the service is accessible.If the port is listening, it usually means that the AnQiCMS application has started successfully and is ready to accept connections.Therefore, by combining PID and port monitoring, we can more comprehensively and accurately evaluate the operation status of AnQiCMS, and promptly detect and resolve potential service interruption issues.

Prerequisites for monitoring scripts

Before starting to write scripts, please make sure that the following common tools are installed on your Linux server:

  • Bashas the execution environment for scripts, usually pre-installed by the system.
  • pgrep: a tool used to find process IDs, moreps -ef | grepsimple and efficient than combination.
  • ss:socket statisticsfor displaying socket statistics information, it can replace the traditionalnetstatcommand, check the port listening status more quickly.

If you are using an older system or lack these tools, you can install them through the package manager, for example on Debian/Ubuntusudo apt install procps iproute2Use on CentOS/RHELsudo yum install procps-ng iproute-tc.

Write AnQiCMS health check script

Here is an example of a Bash script that combines PID and port detection, you can adjust it according to your actual situation.

#!/bin/bash

# AnQiCMS 健康检查与监控脚本
# 该脚本用于检查 AnQiCMS 进程是否运行以及其配置的端口是否正常监听。

# --- 配置项 ---
# AnQiCMS 可执行文件的名称,默认为 'anqicms'
ANQICMS_BIN_NAME="anqicms"

# AnQiCMS 的安装路径,请根据您的实际部署情况修改
ANQICMS_INSTALL_PATH="/www/wwwroot/anqicms"

# AnQiCMS 监听的端口,默认为 8001。
# 如果您的 AnQiCMS 配置了不同的端口(例如在config.json中修改),请务必在此处更新。
ANQICMS_PORT="8001"

# 监控日志文件路径
# 所有检查结果和状态都将记录到此文件中
LOG_FILE="${ANQICMS_INSTALL_PATH}/monitor.log"

# --- 辅助函数:记录日志 ---
# 记录带时间戳的消息到日志文件,并同时打印到标准输出
log_message() {
    echo "$(date +'%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}

log_message "--- 启动 AnQiCMS 健康检查 ---"

# --- 第一步:检查 AnQiCMS 进程是否存在 (PID 检测) ---
log_message "正在检查 AnQiCMS 进程 (PID)..."
# 使用 pgrep -x 查找精确匹配的可执行文件名,避免误判
PID=$(pgrep -x "$ANQICMS_BIN_NAME")

if [ -n "$PID" ]; then
    log_message "AnQiCMS 进程 '$ANQICMS_BIN_NAME' 正在运行,PID: $PID"
    PID_STATUS="OK"
else
    log_message "错误:AnQiCMS 进程 '$ANQICMS_BIN_NAME' 未运行。"
    PID_STATUS="FAILED"
fi

# --- 第二步:检查配置的端口是否正在监听 ---
log_message "正在检查端口 $ANQICMS_PORT 是否正在监听..."
# 使用 ss -tulnp 检查 TCP/UDP 监听端口,-c 计数匹配行
# 注意:'sport = :${ANQICMS_PORT}' 确保精确匹配监听端口
PORT_LISTENING_COUNT=$(ss -tuln "sport = :${ANQICMS_PORT}" | grep -c "LISTEN")

if [ "$PORT_LISTENING_COUNT" -gt 0 ]; then
    log_message "端口 $ANQICMS_PORT 正在监听。"
    PORT_STATUS="OK"
else
    log_message "错误:端口 $ANQICMS_PORT 未监听。"
    PORT_STATUS="FAILED"
fi

# --- 第三步:综合评估 AnQiCMS 的整体健康状态 ---
log_message "--- AnQiCMS 健康检查总结 ---"
if [ "$PID_STATUS" == "OK" ] && [ "$PORT_STATUS" == "OK" ]; then
    log_message "状态:AnQiCMS 运行正常,端口 $ANQICMS_PORT 活跃。 (健康)"
    exit 0 # 脚本以0退出码表示健康
else
    log_message "状态:AnQiCMS 存在异常,请检查! (不健康)"
    exit 1 # 脚本以非0退出码表示不健康
fi

Configure and run the script

Save the above code to a file, for examplemonitor_anqicms.shand place it in the AnQiCMS installation directory (or any directory you manage conveniently).

Before running, you need to modify the script according to the actual situation in it.ANQICMS_BIN_NAME/ANQICMS_INSTALL_PATHandANQICMS_PORTEspecially the variables.ANQICMS_PORTIf the port of your AnQiCMS is not the default 8001, please be sure to modify it.

Next, add execute permission to the script:chmod +x /path/to/your/monitor_anqicms.sh

Then you can manually run the script for testing:/path/to/your/monitor_anqicms.sh

The output of the script will be displayed in the terminal and also appended to the one you configuredLOG_FILE.

Integrate the monitoring script into the cron scheduled task

To achieve continuous automated monitoring, we strongly recommend adding this script to the system's scheduled task (Cron Job). For example, if you want to check the status of AnQiCMS every 5 minutes:

First, open your Cron table for editing:crontab -e

Then, add the following line at the end of the file:*/5 * * * * /path/to/your/monitor_anqicms.sh > /dev/null 2>&1

This line of configuration indicates that it will run every 5 minutesmonitor_anqicms.shScript, and redirect all standard output and error output to/dev/nullTo avoid frequent email notifications, all logs will be written to the specified by the script internallog_messageFunction to the specifiedmonitor.logIn the file. When the system detects an abnormality in AnQiCMS, you can checkmonitor.logor combine it with the subsequent alarm mechanism to receive notifications.

Conclusion

By writing and deploying such an AnQiCMS monitoring script, you will be able to more actively and accurately grasp the health status of the website service.It provides a solid foundation, helping you gain insights at the first sign of a problem, thereby minimizing potential service downtime and ensuring the efficient and stable operation of your AnQiCMS website.

Frequently Asked Questions (FAQ)

1. How often should I run this monitoring script?

The frequency of the script's operation depends on the sensitivity of your requirements for service availability.For most AnQiCMS websites, running once every 1 to 5 minutes is appropriate.Too frequent (for example, every 30 seconds) may increase system load, while too long an interval (such as every hour) may lead to timely discovery of problems.You can weigh and adjust according to the traffic of the website, the importance of the business, and the server resources.

2. How can I receive real-time notifications after the script detects an issue?

This monitoring script defaults to writing status information to the log file.To implement real-time notifications, you can combine some external tools.

Related articles

What is the meaning of `PID check: 0` in the `check.log` after the AnQiCMS process exits abnormally?

As an experienced CMS website operation personnel of an enterprise, I know the importance of stable system operation for the content platform.In daily work, we often need to pay attention to various system logs in order to discover and solve potential problems in a timely manner.Among them, the `PID check: 0` record in the `check.log` file is a very critical signal when we are troubleshooting the abnormal process of AnQiCMS.

2025-11-06

If the AnQiCMS process cannot be terminated with `kill -9`, could it be related to the PID?

As a senior CMS website operation personnel of a well-known security company, I know that process control is a key link in maintaining the stable operation of the website in daily management.When encountering a situation where it is necessary to forcibly terminate the AnQiCMS process but the `kill -9` command seems to be ineffective, this is indeed a headache.Below, I will elaborate on the possible reasons from my experience and discuss the role of process ID (PID) in it.

2025-11-06

What changes in the PID check and management logic when deploying AnQiCMS in the Docker environment?

In the operation practice of AnQi CMS, efficient content creation, publishing, and optimization are the core, but the guarantee of stable system operation is also indispensable.This is an important link in system maintenance, checking and managing the process (PID).When AnQi CMS migrates from the traditional server deployment environment to the Docker containerized environment, this underlying PID management logic will undergo a fundamental change, which is crucial for website operators to understand and troubleshoot problems.

2025-11-06

After upgrading AnQiCMS, will the PID of the old process be automatically updated, or do I need to handle it manually?

As an experienced operator of AnQi CMS, I am well aware of the importance of system upgrades for maintaining website security and advanced functionality.When performing an AnQiCMS upgrade, many users may have a core question: Will the old program process identifier (PID) be automatically updated after the upgrade?Or should we manually intervene? AnQiCMS is an enterprise-level content management system developed based on the Go programming language, one of its core features is to run as a standalone binary executable file.This means when you start AnQiCMS

2025-11-06

What impact will there be on the PID management if `cd $BINPATH` fails when AnQiCMS starts?

As an experienced CMS website operation person in an enterprise, I am well aware of the importance of system stability for content release and user experience.AnQi CMS as an efficient Go language content management system, its startup mechanism, especially PID management, is a key link to ensure the service is online continuously.When the `cd $BINPATH` command in the startup script fails, it will cause a cascade effect on the subsequent process ID (PID) management, which in turn will affect the normal operation of the entire CMS.The startup script of AnQi CMS, such as the `start.sh` mentioned in the document

2025-11-06

How to ensure that the process keeps running after the system restarts when deploying AnQiCMS manually in the command line, by adding `start.sh` to `crontab`?

As an expert in the operation of Anqing CMS, I fully understand the importance of stable system operation for content management, especially under complex and changing server environments.For users who manually deploy AnQiCMS via the command line, ensuring that the service can automatically recover after system restart is a critical link in ensuring the continuous online operation of the website.The following will elaborate on how AnQiCMS achieves this goal through the collaborative work of `crontab` and `start.sh` scripts.

2025-11-06

How does the Baota panel's Go project deployment method differ from the PID management mechanism of the AnQiCMS built-in script?

AnQi CMS is a corporate-level content management system developed based on the Go language, which is favored by many small and medium-sized enterprises and content operation teams for its easy deployment, security and efficiency.In the Linux server environment, Baota panel, as a widely popular server operation and maintenance tool, provides great convenience for the deployment of AnQiCMS.This article will discuss in detail how to deploy the AnQiCMS Go project on the Baota panel, and analyze the similarities and differences in its PID management mechanism and the scripts provided by AnQiCMS.### Panel deployment of AnQiCMS Go

2025-11-06

In the AnQiCMS multi-site deployment tutorial, how is the PID of different sites distinguished and managed?

As an experienced security CMS website operator, I fully understand the great value of multi-site deployment in content management and efficiency improvement.Regarding your question about how different site PIDs are distinguished and managed in the AnQiCMS multi-site deployment tutorial?This question, I will elaborate on the mechanism of Anqi CMS design concept and practical operation for you.In the multi-site deployment scenario of AnQi CMS, distinguishing and managing the PID (Process ID, process ID) of different sites mainly depends on the deployment method you adopt

2025-11-06