Why is it necessary to monitor processes and ports simultaneously?
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, more
ps -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.