mirror of
https://github.com/hotyue/IP-Sentinel.git
synced 2026-05-10 17:52:41 +08:00
refactor(core): 缝合 Systemd 架构,修复 PR #25 中的管道符闪退及 oneshot 守护进程死锁漏洞
This commit is contained in:
@@ -54,12 +54,12 @@
|
||||
|
||||
- **部署 Master (中枢大脑)**:找一台 VPS 作为司令部(仅需部署一台),执行:
|
||||
```bash
|
||||
curl -sL https://raw.githubusercontent.com/hotyue/IP-Sentinel/main/master/install_master.sh | sudo bash
|
||||
sudo bash -c "$(curl -fsSL https://raw.githubusercontent.com/hotyue/IP-Sentinel/main/master/install_master.sh)"
|
||||
```
|
||||
- 部署 Agent (边缘哨兵):在需要养护的机器上执行 Agent 脚本,安装时选择私有独立中枢,并分别输入您自建机器人的 [Token](https://blog.iot-architect.com/engineering-practice/create-private-telegram-bot-via-botfather) 以及您的个人 [Chat ID](https://blog.iot-architect.com/engineering-practice/get-telegram-personal-id-via-userinfobot) :
|
||||
|
||||
```Bash
|
||||
curl -sL https://raw.githubusercontent.com/hotyue/IP-Sentinel/main/core/install.sh | sudo bash
|
||||
sudo bash -c "$(curl -fsSL https://raw.githubusercontent.com/hotyue/IP-Sentinel/main/core/install.sh)"
|
||||
```
|
||||
- 激活节点:安装完成后,您的手机会收到一条 #REGISTER# 注册暗号,将其转发给您自己的机器人即可完成编队入库。
|
||||
|
||||
@@ -71,7 +71,7 @@ curl -sL https://raw.githubusercontent.com/hotyue/IP-Sentinel/main/core/install.
|
||||
- 部署 Agent:在目标 VPS 上执行以下指令,安装过程中选择官方公共网关,并输入您的 Chat ID:
|
||||
|
||||
```Bash
|
||||
curl -sL https://raw.githubusercontent.com/hotyue/IP-Sentinel/main/core/install.sh | sudo bash
|
||||
sudo bash -c "$(curl -fsSL https://raw.githubusercontent.com/hotyue/IP-Sentinel/main/core/install.sh)"
|
||||
```
|
||||
- 激活节点:同上,将收到的暗号转发给官方机器人即可。
|
||||
|
||||
@@ -106,7 +106,7 @@ bash /opt/ip_sentinel/core/uninstall.sh
|
||||
*(注意:该分支仅作基础维护,不享受新功能迭代,请尽可能升级你的系统)*
|
||||
|
||||
```bash
|
||||
curl -sL https://raw.githubusercontent.com/hotyue/IP-Sentinel/legacy/core/install.sh | sudo bash
|
||||
sudo bash -c "$(curl -fsSL https://raw.githubusercontent.com/hotyue/IP-Sentinel/legacy/core/install.sh)"
|
||||
```
|
||||
|
||||
## 📡 战术联络 (Community)
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
# ==========================================================
|
||||
if [ "$EUID" -ne 0 ]; then
|
||||
echo -e "\033[31m❌ 权限被拒绝: 部署 IP-Sentinel 需要最高系统权限。\033[0m"
|
||||
echo -e "💡 请使用 \033[36msudo bash $0\033[0m 或切换到 root 用户 (su root) 后重新执行指令。"
|
||||
echo -e "💡 请使用 \033[36msudo bash -c \"\$(curl -fsSL ...)\"\033[0m 或切换到 root 执行。"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -638,17 +638,19 @@ echo $(date +%s) > "${INSTALL_DIR}/core/.ua_last_update"
|
||||
# [修复竞态]: 提前写入公网 IP 缓存,彻底阻断 agent_daemon 首次启动时的抢跑推送
|
||||
echo "$SAFE_PUBLIC_IP" > "${INSTALL_DIR}/core/.last_ip"
|
||||
|
||||
# 7. 配置系统定时任务 (高频调度与看门狗)
|
||||
echo -e "\n[7/7] 正在注入系统定时任务与看门狗进程..."
|
||||
if command -v systemctl >/dev/null 2>&1; then
|
||||
echo "💡 检测到 Systemd 环境,正在部署 Systemd 服务单元..."
|
||||
# 7. 配置系统任务与原生守护进程
|
||||
echo -e "\n[7/7] 正在注入系统任务与守护进程..."
|
||||
crontab -l 2>/dev/null | grep -v "ip_sentinel" > /tmp/cron_backup || true
|
||||
|
||||
# 1. 编写 Runner 核心养护模块服务
|
||||
echo $(date +%s) > "${INSTALL_DIR}/core/.ua_last_update"
|
||||
|
||||
if command -v systemctl >/dev/null 2>&1; then
|
||||
echo "💡 检测到 Systemd 环境,正在部署 Systemd 调度单元..."
|
||||
|
||||
cat > /etc/systemd/system/ip-sentinel-runner.service << EOF
|
||||
[Unit]
|
||||
Description=IP-Sentinel Runner Service
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
|
||||
SyslogIdentifier=ip-sentinel
|
||||
@@ -659,28 +661,23 @@ CPUSchedulingPolicy=idle
|
||||
IOSchedulingClass=idle
|
||||
EOF
|
||||
|
||||
# 2. 编写 Runner 定时器 (每 30 分钟): 每次运行前随机休眠 0 到 180 秒
|
||||
cat > /etc/systemd/system/ip-sentinel-runner.timer << EOF
|
||||
[Unit]
|
||||
Description=Timer for IP-Sentinel Runner Service
|
||||
|
||||
[Timer]
|
||||
OnBootSec=10
|
||||
OnUnitActiveSec=30min
|
||||
RandomizedDelaySec=180
|
||||
Persistent=true
|
||||
Unit=ip-sentinel-runner.service
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
||||
EOF
|
||||
|
||||
# 3. 编写 Updater 养料更新模块服务
|
||||
cat > /etc/systemd/system/ip-sentinel-updater.service << EOF
|
||||
[Unit]
|
||||
Description=IP-Sentinel Updater Service
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
|
||||
SyslogIdentifier=ip-sentinel
|
||||
@@ -691,35 +688,28 @@ CPUSchedulingPolicy=idle
|
||||
IOSchedulingClass=idle
|
||||
EOF
|
||||
|
||||
# 4. 编写 Updater 定时器 (每天凌晨 3 点触发)
|
||||
cat > /etc/systemd/system/ip-sentinel-updater.timer << EOF
|
||||
[Unit]
|
||||
Description=Timer for IP-Sentinel Updater Service
|
||||
|
||||
[Timer]
|
||||
OnCalendar=*-*-* 03:00:00
|
||||
Persistent=true
|
||||
Unit=ip-sentinel-updater.service
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
||||
EOF
|
||||
|
||||
# 重载服务配置并启动服务
|
||||
systemctl daemon-reload
|
||||
systemctl enable --now ip-sentinel-runner.timer
|
||||
systemctl enable --now ip-sentinel-updater.timer
|
||||
# 强制重载服务,兼容 OTA
|
||||
systemctl restart ip-sentinel-runner.timer ip-sentinel-updater.timer
|
||||
|
||||
# 如果配置了联控,启动 Webhook 与战报任务
|
||||
if [[ -n "$TG_TOKEN" ]] && [[ -n "$CHAT_ID" ]]; then
|
||||
# 编写 TG 战报服务
|
||||
echo "$SAFE_PUBLIC_IP" > "${INSTALL_DIR}/core/.last_ip"
|
||||
|
||||
cat > /etc/systemd/system/ip-sentinel-report.service << EOF
|
||||
[Unit]
|
||||
Description=IP-Sentinel Telegram Report Service
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
|
||||
SyslogIdentifier=ip-sentinel
|
||||
@@ -730,82 +720,55 @@ CPUSchedulingPolicy=idle
|
||||
IOSchedulingClass=idle
|
||||
EOF
|
||||
|
||||
# 编写 TG 战报服务定时器 (每天早上 8 点触发)
|
||||
cat > /etc/systemd/system/ip-sentinel-report.timer << EOF
|
||||
[Unit]
|
||||
Description=Timer for IP-Sentinel Telegram Report Service
|
||||
|
||||
Description=Timer for IP-Sentinel Telegram Report
|
||||
[Timer]
|
||||
OnCalendar=*-*-* 08:00:00
|
||||
Unit=ip-sentinel-report.service
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
||||
EOF
|
||||
|
||||
# 编写 Agent 进程守护服务
|
||||
# ⚠️ 已修复陷阱:改为 Type=simple,去除 Timer
|
||||
cat > /etc/systemd/system/ip-sentinel-agent-daemon.service << EOF
|
||||
[Unit]
|
||||
Description=IP-Sentinel Agent Daemon Service
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
|
||||
SyslogIdentifier=ip-sentinel
|
||||
Type=oneshot
|
||||
Type=simple
|
||||
ExecStart=/bin/bash ${INSTALL_DIR}/core/agent_daemon.sh
|
||||
KillMode=process
|
||||
Restart=always
|
||||
RestartSec=10
|
||||
User=root
|
||||
CPUSchedulingPolicy=idle
|
||||
IOSchedulingClass=idle
|
||||
EOF
|
||||
|
||||
# 编写 Agent Daemon 定时器 (每 1 分钟)
|
||||
cat > /etc/systemd/system/ip-sentinel-agent-daemon.timer << EOF
|
||||
[Unit]
|
||||
Description=Timer for IP-Sentinel Agent Daemon Service
|
||||
|
||||
[Timer]
|
||||
OnBootSec=10
|
||||
OnUnitActiveSec=1min
|
||||
Unit=ip-sentinel-agent-daemon.service
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
||||
WantedBy=multi-user.target
|
||||
EOF
|
||||
|
||||
# 重载服务配置并启动服务
|
||||
systemctl daemon-reload
|
||||
systemctl enable --now ip-sentinel-report.timer
|
||||
systemctl enable --now ip-sentinel-agent-daemon.timer
|
||||
# 安装时立刻启动一次边缘守护进程
|
||||
systemctl start ip-sentinel-agent-daemon.service
|
||||
# 强制重载服务,兼容 OTA
|
||||
systemctl restart ip-sentinel-report.timer ip-sentinel-agent-daemon.timer
|
||||
systemctl enable --now ip-sentinel-agent-daemon.service
|
||||
systemctl restart ip-sentinel-report.timer ip-sentinel-agent-daemon.service
|
||||
fi
|
||||
|
||||
[ -f /tmp/cron_backup ] && crontab /tmp/cron_backup 2>/dev/null
|
||||
rm -f /tmp/cron_backup
|
||||
else
|
||||
echo "💡 未检测到 Systemd (可能是 Alpine Linux),回退到 Cron 调度模式..."
|
||||
|
||||
crontab -l 2>/dev/null | grep -v "ip_sentinel" > /tmp/cron_backup || true
|
||||
|
||||
# 核心养护模块: 每 30 分钟触发一次
|
||||
echo "*/30 * * * * ${INSTALL_DIR}/core/runner.sh >/dev/null 2>&1" >> /tmp/cron_backup
|
||||
# 养料更新模块: (v3.3.0升级) 每天凌晨 3 点触发,由中枢自动进行分频调度
|
||||
echo "0 3 * * * ${INSTALL_DIR}/core/updater.sh >/dev/null 2>&1" >> /tmp/cron_backup
|
||||
|
||||
# 如果配置了联控,启动 Webhook 与战报任务
|
||||
if [[ -n "$TG_TOKEN" ]] && [[ -n "$CHAT_ID" ]]; then
|
||||
# 每天早上 8 点发送昨天的统计战报
|
||||
echo "$SAFE_PUBLIC_IP" > "${INSTALL_DIR}/core/.last_ip"
|
||||
echo "0 8 * * * ${INSTALL_DIR}/core/tg_report.sh >/dev/null 2>&1" >> /tmp/cron_backup
|
||||
|
||||
# 双保险守护进程看门狗
|
||||
echo "@reboot nohup bash ${INSTALL_DIR}/core/agent_daemon.sh >/dev/null 2>&1 &" >> /tmp/cron_backup
|
||||
echo "* * * * * nohup bash ${INSTALL_DIR}/core/agent_daemon.sh >/dev/null 2>&1 &" >> /tmp/cron_backup
|
||||
|
||||
# 安装时立刻启动一次边缘守护进程
|
||||
echo "* * * * * pgrep -f agent_daemon.sh >/dev/null || nohup bash ${INSTALL_DIR}/core/agent_daemon.sh >/dev/null 2>&1 &" >> /tmp/cron_backup
|
||||
nohup bash "${INSTALL_DIR}/core/agent_daemon.sh" >/dev/null 2>&1 &
|
||||
fi
|
||||
|
||||
[ -f /tmp/cron_backup ] && crontab /tmp/cron_backup 2>/dev/null
|
||||
rm -f /tmp/cron_backup
|
||||
fi
|
||||
|
||||
@@ -22,18 +22,13 @@ if ! type log >/dev/null 2>&1; then
|
||||
# [v3.4.0 核心] 提取当前配置中的版本锚点
|
||||
local local_ver="${AGENT_VERSION:-未知}"
|
||||
|
||||
# 保证日志目录存在
|
||||
mkdir -p "${INSTALL_DIR}/logs"
|
||||
|
||||
# 日志格式注入 [版本号] 追踪标识
|
||||
local core_msg=$(printf "[v%-5s] [%-5s] [%-7s] [%s] %s" "$local_ver" "$2" "$1" "$REGION_CODE" "$3")
|
||||
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $core_msg" >> "${INSTALL_DIR}/logs/sentinel.log"
|
||||
|
||||
# 强制推送到 Systemd Journal (如果系统支持)
|
||||
if command -v logger >/dev/null 2>&1; then
|
||||
logger -t ip-sentinel "$core_msg"
|
||||
else
|
||||
# 降级输出到 stdout,让 Systemd 捕获
|
||||
echo "$core_msg"
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -31,18 +31,12 @@ log() {
|
||||
# [v3.4.0 核心] 提取当前配置中的版本锚点
|
||||
local local_ver="${AGENT_VERSION:-未知}"
|
||||
|
||||
# 保证日志目录存在
|
||||
mkdir -p "${INSTALL_DIR}/logs"
|
||||
|
||||
# 日志格式注入 [版本号] 追踪标识
|
||||
local core_msg=$(printf "[v%-5s] [%-5s] [%-7s] [%s] %s" "$local_ver" "$level" "$module" "$REGION_CODE" "$msg")
|
||||
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $core_msg" >> "$LOG_FILE"
|
||||
|
||||
# 强制推送到 Systemd Journal (如果系统支持)
|
||||
if command -v logger >/dev/null 2>&1; then
|
||||
logger -t ip-sentinel "$core_msg"
|
||||
else
|
||||
# 降级输出到 stdout,让 Systemd 捕获
|
||||
echo "$core_msg"
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -5,12 +5,9 @@
|
||||
# 核心功能: 无痕清理守护进程、定时任务、运行目录及临时缓存
|
||||
# ==========================================================
|
||||
|
||||
# ==========================================================
|
||||
# 🛑 核心权限防线: 检查是否以 root 权限运行
|
||||
# ==========================================================
|
||||
if [ "$EUID" -ne 0 ]; then
|
||||
echo -e "\033[31m❌ 权限被拒绝: 卸载 IP-Sentinel 需要最高系统权限。\033[0m"
|
||||
echo -e "💡 请使用 \033[36msudo bash $0\033[0m 或切换到 root 用户 (su root) 后重新执行指令。"
|
||||
echo -e "💡 请使用 \033[36msudo bash -c \"\$(curl -fsSL ...)\"\033[0m 或切换到 root 执行。"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -19,7 +16,6 @@ INSTALL_DIR="/opt/ip_sentinel"
|
||||
echo "========================================================"
|
||||
echo " 🗑️ 准备卸载 IP-Sentinel (边缘节点 Edge Agent)"
|
||||
|
||||
# [核心: 动态读取并播报即将销毁的本地版本号]
|
||||
CONFIG_FILE="${INSTALL_DIR}/config.conf"
|
||||
if [ -f "$CONFIG_FILE" ]; then
|
||||
CURRENT_VER=$(grep "^AGENT_VERSION=" "$CONFIG_FILE" | cut -d'"' -f2)
|
||||
@@ -27,35 +23,20 @@ if [ -f "$CONFIG_FILE" ]; then
|
||||
fi
|
||||
echo "========================================================"
|
||||
|
||||
# 1. 停止并删除 Systemd 服务
|
||||
echo "[1/4] 正在停止并删除 Systemd 服务..."
|
||||
echo "[1/3] 正在终止后台守护进程与 Systemd 服务..."
|
||||
if command -v systemctl >/dev/null 2>&1; then
|
||||
echo "[1/4] 💡 检测到 Systemd 环境,正在删除 Systemd 服务单元..."
|
||||
systemctl disable --now ip-sentinel-runner.service ip-sentinel-runner.timer \
|
||||
ip-sentinel-updater.service ip-sentinel-updater.timer \
|
||||
ip-sentinel-report.service ip-sentinel-report.timer \
|
||||
ip-sentinel-agent-daemon.service ip-sentinel-agent-daemon.timer >/dev/null 2>&1
|
||||
rm -f /etc/systemd/system/ip-sentinel-runner.service
|
||||
rm -f /etc/systemd/system/ip-sentinel-runner.timer
|
||||
rm -f /etc/systemd/system/ip-sentinel-updater.service
|
||||
rm -f /etc/systemd/system/ip-sentinel-updater.timer
|
||||
rm -f /etc/systemd/system/ip-sentinel-report.service
|
||||
rm -f /etc/systemd/system/ip-sentinel-report.timer
|
||||
rm -f /etc/systemd/system/ip-sentinel-agent-daemon.service
|
||||
rm -f /etc/systemd/system/ip-sentinel-agent-daemon.timer
|
||||
ip-sentinel-agent-daemon.service >/dev/null 2>&1
|
||||
rm -f /etc/systemd/system/ip-sentinel-*.service
|
||||
rm -f /etc/systemd/system/ip-sentinel-*.timer
|
||||
systemctl daemon-reload
|
||||
systemctl reset-failed
|
||||
else
|
||||
echo "[1/4] 💡 未检测到 Systemd,跳过此步骤..."
|
||||
fi
|
||||
|
||||
# 2. 停止运行中的守护进程与主控模块 (涵盖所有历史版本进程)
|
||||
echo "[2/4] 正在终止后台守护进程与所有养护任务..."
|
||||
|
||||
# 使用 pkill 替代传统的 pgrep | xargs,指令更短、容错率更高
|
||||
pkill -9 -f "tg_daemon.sh" >/dev/null 2>&1
|
||||
pkill -9 -f "agent_daemon.sh" >/dev/null 2>&1
|
||||
# [v3.4.0 优化] 确保清理所有 python3 调起的 Webhook 实例
|
||||
pkill -9 -f "python3.*webhook.py" >/dev/null 2>&1
|
||||
pkill -9 -f "webhook.py" >/dev/null 2>&1
|
||||
pkill -9 -f "runner.sh" >/dev/null 2>&1
|
||||
@@ -64,26 +45,22 @@ pkill -9 -f "tg_report.sh" >/dev/null 2>&1
|
||||
pkill -9 -f "mod_google.sh" >/dev/null 2>&1
|
||||
pkill -9 -f "mod_trust.sh" >/dev/null 2>&1
|
||||
|
||||
# 3. 清除系统定时任务 (Cron)
|
||||
echo "[3/4] 正在清理系统定时任务 (Cron)..."
|
||||
echo "[2/3] 正在清理系统定时任务 (Cron)..."
|
||||
if crontab -l >/dev/null 2>&1; then
|
||||
crontab -l | grep -v "ip_sentinel" > /tmp/cron_backup
|
||||
crontab /tmp/cron_backup
|
||||
rm -f /tmp/cron_backup
|
||||
fi
|
||||
|
||||
# 4. 删除所有文件、日志与临时缓存
|
||||
echo "[4/4] 正在抹除核心程序、配置文件与系统痕迹..."
|
||||
echo "[3/3] 正在抹除核心程序、配置文件与系统痕迹..."
|
||||
if [ -d "$INSTALL_DIR" ]; then
|
||||
rm -rf "$INSTALL_DIR"
|
||||
fi
|
||||
|
||||
# 拔除 /tmp 目录下的所有更新下载临时文件和 V1/V2 遗留的偏移量记录
|
||||
rm -f /tmp/ip_sentinel_*.txt
|
||||
rm -f /tmp/ip_sentinel_*.json
|
||||
|
||||
echo "========================================================"
|
||||
echo "✅ 卸载彻底完成!IP-Sentinel 已从您的系统中无痕移除。"
|
||||
echo "💡 提示:如果安装时在防火墙放行了 Webhook 随机端口,请您按需手动关闭。"
|
||||
echo "👋 感谢您的使用,期待未来再次为您守护资产!"
|
||||
echo "💡 提示:如果安装时在防火墙放行了 Webhook 随机端口,请按需手动关闭。"
|
||||
echo "========================================================"
|
||||
@@ -25,18 +25,12 @@ log() {
|
||||
# [v3.4.0 核心] 提取当前配置中的版本锚点
|
||||
local local_ver="${AGENT_VERSION:-未知}"
|
||||
|
||||
# 保证日志目录存在
|
||||
mkdir -p "${INSTALL_DIR}/logs"
|
||||
|
||||
# 日志格式注入 [版本号] 追踪标识
|
||||
local core_msg=$(printf "[v%-5s] [%-5s] [%-7s] [%s] %s" "$local_ver" "$2" "$1" "$REGION_CODE" "$3")
|
||||
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $core_msg" >> "$LOG_FILE"
|
||||
|
||||
# 强制推送到 Systemd Journal (如果系统支持)
|
||||
if command -v logger >/dev/null 2>&1; then
|
||||
logger -t ip-sentinel "$core_msg"
|
||||
else
|
||||
# 降级输出到 stdout,让 Systemd 捕获
|
||||
echo "$core_msg"
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
# ==========================================================
|
||||
if [ "$EUID" -ne 0 ]; then
|
||||
echo -e "\033[31m❌ 权限被拒绝: 部署 IP-Sentinel 需要最高系统权限。\033[0m"
|
||||
echo -e "💡 请使用 \033[36msudo bash $0\033[0m 或切换到 root 用户 (su root) 后重新执行指令。"
|
||||
echo -e "💡 请使用 \033[36msudo bash -c \"\$(curl -fsSL ...)\"\033[0m 或切换到 root 执行。"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -265,14 +265,11 @@ chmod 600 "$DB_FILE"
|
||||
|
||||
# 4. 拉取核心调度代码并运行
|
||||
echo -e "\n[4/4] 部署 TG 调度守护进程..."
|
||||
# [修改] 剥离了写死的网址,改用顶部的 ${REPO_RAW_URL} 变量,确保与卸载脚本的数据源同源
|
||||
curl -sL "${REPO_RAW_URL}/master/tg_master.sh" -o "${MASTER_DIR}/tg_master.sh"
|
||||
chmod +x "${MASTER_DIR}/tg_master.sh"
|
||||
|
||||
if command -v systemctl >/dev/null 2>&1; then
|
||||
echo "💡 检测到 Systemd 环境,正在部署 Systemd 服务单元..."
|
||||
|
||||
# 部署 Master 战队主控调度服务
|
||||
echo "💡 检测到 Systemd 环境,正在部署原生守护服务..."
|
||||
cat > /etc/systemd/system/ip-sentinel-master.service << EOF
|
||||
[Unit]
|
||||
Description=IP-Sentinel Master Command Center Service
|
||||
@@ -293,23 +290,20 @@ IOSchedulingClass=idle
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
EOF
|
||||
|
||||
# 重载服务配置并启动服务
|
||||
systemctl daemon-reload
|
||||
# 立刻启用 Master 战队主控调度服务
|
||||
systemctl enable --now ip-sentinel-master.service
|
||||
# 强制重载服务,兼容 OTA
|
||||
systemctl restart ip-sentinel-master.service
|
||||
|
||||
# 清理遗留的 Cron
|
||||
crontab -l 2>/dev/null | grep -v "tg_master.sh" > /tmp/cron_master || true
|
||||
[ -f /tmp/cron_master ] && crontab /tmp/cron_master 2>/dev/null
|
||||
rm -f /tmp/cron_master
|
||||
else
|
||||
echo "💡 未检测到 Systemd (可能是 Alpine Linux),回退到 Cron 调度模式..."
|
||||
|
||||
# 写入看门狗 Cron (容错版)
|
||||
echo "💡 未检测到 Systemd,回退到 Cron 看门狗调度模式..."
|
||||
crontab -l 2>/dev/null | grep -v "tg_master.sh" > /tmp/cron_master || true
|
||||
echo "* * * * * pgrep -f tg_master.sh >/dev/null || nohup bash ${MASTER_DIR}/tg_master.sh >/dev/null 2>&1 &" >> /tmp/cron_master
|
||||
[ -f /tmp/cron_master ] && crontab /tmp/cron_master 2>/dev/null
|
||||
rm -f /tmp/cron_master
|
||||
|
||||
# 立刻启动 (追加 disown 彻底脱离终端管控,实现绝对静默)
|
||||
pgrep -f tg_master.sh >/dev/null || { nohup bash "${MASTER_DIR}/tg_master.sh" >/dev/null 2>&1 & disown 2>/dev/null; }
|
||||
fi
|
||||
|
||||
|
||||
@@ -1,16 +1,13 @@
|
||||
#!/bin/bash
|
||||
|
||||
# ==========================================================
|
||||
# 脚本名称: uninstall_master.sh (IP-Sentinel Master 一键卸载脚本 - 动态锚点版)
|
||||
# 脚本名称: uninstall_master.sh (IP-Sentinel Master 一键卸载脚本)
|
||||
# 核心功能: 终止调度进程、清理看门狗定时任务、抹除数据库与配置
|
||||
# ==========================================================
|
||||
|
||||
# ==========================================================
|
||||
# 🛑 核心权限防线: 检查是否以 root 权限运行
|
||||
# ==========================================================
|
||||
if [ "$EUID" -ne 0 ]; then
|
||||
echo -e "\033[31m❌ 权限被拒绝: 卸载 IP-Sentinel 需要最高系统权限。\033[0m"
|
||||
echo -e "💡 请使用 \033[36msudo bash $0\033[0m 或切换到 root 用户 (su root) 后重新执行指令。"
|
||||
echo -e "💡 请使用 \033[36msudo bash -c \"\$(curl -fsSL ...)\"\033[0m 执行。"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -20,7 +17,6 @@ CONF_FILE="${MASTER_DIR}/master.conf"
|
||||
echo "========================================================"
|
||||
echo " 🗑️ 准备卸载 IP-Sentinel Master (控制中枢)"
|
||||
|
||||
# [v3.4.0 优化] 卸载前读取并播报中枢版本号
|
||||
if [ -f "$CONF_FILE" ]; then
|
||||
MASTER_VER=$(grep "^MASTER_VERSION=" "$CONF_FILE" | cut -d'"' -f2)
|
||||
[ -n "$MASTER_VER" ] && echo " 📍 目标版本: v${MASTER_VER}"
|
||||
@@ -34,32 +30,22 @@ if [[ ! "$CONFIRM_DEL" =~ ^[Yy]$ ]]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# 1. 停止并删除 Systemd 服务
|
||||
echo "[1/4] 正在停止并删除 Systemd 服务..."
|
||||
echo "[1/3] 正在终止后台中枢 Systemd 调度进程..."
|
||||
if command -v systemctl >/dev/null 2>&1; then
|
||||
echo "[1/4] 💡 检测到 Systemd 环境,正在删除 Systemd 服务单元..."
|
||||
|
||||
systemctl disable --now ip-sentinel-master.service >/dev/null 2>&1
|
||||
rm -f /etc/systemd/system/ip-sentinel-master.service
|
||||
systemctl daemon-reload
|
||||
systemctl reset-failed
|
||||
else
|
||||
echo "[1/4] 💡 未检测到 Systemd,跳过此步骤..."
|
||||
fi
|
||||
|
||||
# 2. 停止运行中的 Master 守护进程
|
||||
echo "[2/4] 正在终止后台中枢调度进程..."
|
||||
# [优化] 使用 pkill 替代 pgrep | xargs,指令更短、容错率更高
|
||||
pkill -9 -f "tg_master.sh" >/dev/null 2>&1 || true
|
||||
|
||||
# 3. 清除看门狗定时任务 (Cron)
|
||||
echo "[3/4] 正在清理系统定时任务 (Cron)..."
|
||||
echo "[2/3] 正在清理看门狗定时任务 (Cron)..."
|
||||
crontab -l 2>/dev/null | grep -v "tg_master.sh" > /tmp/cron_backup
|
||||
crontab /tmp/cron_backup
|
||||
rm -f /tmp/cron_backup
|
||||
|
||||
# 4. 删除所有文件、配置与数据库
|
||||
echo "[4/4] 正在抹除核心程序、配置文件与 SQLite 数据库..."
|
||||
echo "[3/3] 正在抹除核心程序、配置文件与 SQLite 数据库..."
|
||||
if [ -d "$MASTER_DIR" ]; then
|
||||
rm -rf "$MASTER_DIR"
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user