Automated Interface Monitoring Script with Cron and Enterprise WeChat Bot
This guide explains how to create a Bash script that periodically checks multiple HTTP endpoints on a Linux server, logs the results, and sends failure alerts to an Enterprise WeChat robot, with setup steps, code examples, and a crontab schedule for automation.
The author describes deploying an automatic interface health‑check script on a production server that periodically probes multiple HTTP endpoints and reports abnormal status via an Enterprise WeChat robot.
First, create a working directory and log file:
# mkdir -p /opt/interface-check
# cd /opt/interface-check/
# touch interface.logThen write the Bash script jkdz-check.sh:
#!/bin/bash
#1、Beijing interface address
http_beijing_addr=YOUR_BEIJING_ENDPOINT
#2、XXX enterprise interface address
http_xxxqiye_addr=YOUR_XXX_ENDPOINT
# Enterprise WeChat robot webhook (replace with your actual URL)
WEBHOOK_URL=YOUR_WEBHOOK_URL
while :
do
date=$(date +%Y-%m-%d-%H:%M:%S)
# Check Beijing endpoint
beijing_status_code=`curl -m 20 -s -o /dev/null -w %{http_code} $http_beijing_addr`
if [ "$beijing_status_code" -ne 200 ]; then
curl --location --request POST ${WEBHOOK_URL} \
--header 'Content-Type: application/json' \
-d '{"msgtype": "text","text": {"content": "'$date' 北京-接口连接异常"}}'
echo "$date 北京-接口连接异常" >>/opt/interface-check/interface.log
else
echo "$date 北京-接口连接正常" >>/opt/interface-check/interface.log
fi
# Check XXX enterprise endpoint
xxxqiye_status_code=`curl -m 20 -s -o /dev/null -w %{http_code} $http_xxxqiye_addr`
if [ "$xxxqiye_status_code" -ne 200 ]; then
curl --location --request POST ${WEBHOOK_URL} \
--header 'Content-Type: application/json' \
-d '{"msgtype": "text","text": {"content": "'$date' xxx企业-接口连接异常"}}'
echo "$date xxx企业-接口连接异常" >>/opt/interface-check/interface.log
else
echo "$date xxx企业-接口连接正常" >>/opt/interface-check/interface.log
fi
exit
doneThe script logs timestamps and status messages to /opt/interface-check/interface.log and sends a JSON payload to the WeChat robot whenever a check fails.
To configure the robot, add it to a WeChat group, obtain its webhook URL from the robot details page, and use a simple curl command to test message delivery:
curl 'YOUR_WEBHOOK_URL' \
-H 'Content-Type: application/json' \
-d '{
"msgtype": "text",
"text": {"content": "hello world"}
}'After verifying the script syntax (e.g., with bash -n jkdz-check.sh), run it in the background using nohup:
# nohup bash /opt/interface-check/jkdz-check.sh &
# more nohup.outFinally, schedule the script to run every two hours with a crontab entry:
crontab -e
0 */2 * * * sh /opt/interface-check/jkdz-check.shThe WeChat group receives alert messages when an endpoint is unreachable, and the log file shows timestamped records of both successful and failed checks.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
IT Services Circle
Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
