From 7972b0652828eb589cfe736b86342c0bf0173435 Mon Sep 17 00:00:00 2001 From: hubian <908234780@qq.com> Date: Thu, 26 Mar 2026 17:17:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E6=9E=81=E7=AE=80?= =?UTF-8?q?=E7=89=88=E8=BF=9B=E7=A8=8B=E7=9B=91=E6=8E=A7=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=20simple=5Fmonitor.sh?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 只需修改3处配置:检测间隔、检查命令、执行命令 - 代码简洁,易于理解和修改 - 适合快速部署使用 --- README.md | 3 ++- scripts/simple_monitor.sh | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100755 scripts/simple_monitor.sh diff --git a/README.md b/README.md index 3c366e8..ab05e65 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,8 @@ tools/ | 名称 | 路径 | 说明 | |------|------|------| -| proc_monitor.sh | scripts/proc_monitor.sh | 进程监控脚本,进程消失时执行指定命令 | +| proc_monitor.sh | scripts/proc_monitor.sh | 进程监控脚本(功能完整版) | +| simple_monitor.sh | scripts/simple_monitor.sh | 进程监控脚本(极简版,只需改3行配置) | --- diff --git a/scripts/simple_monitor.sh b/scripts/simple_monitor.sh new file mode 100755 index 0000000..72d54d8 --- /dev/null +++ b/scripts/simple_monitor.sh @@ -0,0 +1,18 @@ +#!/bin/bash +# 简单进程监控脚本 +# 每隔几秒检查一次进程,进程消失则执行命令 + +# ==================== 配置区域(只需改这里) ==================== +CHECK_INTERVAL=10 # 每隔多少秒检查一次 +CHECK_CMD="pgrep myapp" # 检查命令(进程存在应返回0) +EXEC_CMD="echo 'Process died'" # 进程消失后执行的命令 +# =============================================================== + +while true; do + if ! eval "$CHECK_CMD" >/dev/null 2>&1; then + echo "[$(date '+%H:%M:%S')] Process not found, executing: $EXEC_CMD" + eval "$EXEC_CMD" + break + fi + sleep "$CHECK_INTERVAL" +done