2026-06-07 11:05:26 +08:00
|
|
|
|
# MineSentinel 部署指南
|
|
|
|
|
|
|
2026-06-07 19:35:17 +08:00
|
|
|
|
```
|
|
|
|
|
|
🛡️ MineSentinel
|
|
|
|
|
|
Minecraft 服务器守护进程
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
MineSentinel 是一个 Minecraft 服务器守护进程,每 1.5 秒检测服务器端口状态,发现崩溃后自动重启,重启后进入冷却期(200s)防止循环重启。
|
2026-06-07 11:05:26 +08:00
|
|
|
|
|
|
|
|
|
|
## 环境要求
|
|
|
|
|
|
|
2026-06-07 19:35:17 +08:00
|
|
|
|
| 项目 | 说明 |
|
|
|
|
|
|
|------|------|
|
|
|
|
|
|
| **操作系统** | Linux(需要 systemd),也支持 Windows(见下方适配指南) |
|
|
|
|
|
|
| **Ruby** | >= 3.0 |
|
|
|
|
|
|
| **权限** | root(systemd 场景) |
|
|
|
|
|
|
| **Minecraft 服务** | 已注册为 systemd 服务,默认服务名 `minecraft` |
|
2026-06-07 11:05:26 +08:00
|
|
|
|
|
|
|
|
|
|
## 文件结构
|
|
|
|
|
|
|
|
|
|
|
|
```
|
2026-06-07 19:35:17 +08:00
|
|
|
|
/opt/minecraft/sentinel/ # 推荐部署目录
|
|
|
|
|
|
├── main.rb # 主程序入口 —— 检测循环 + 冷却期 + 初始宽限期
|
|
|
|
|
|
├── method_executor.rb # 日志系统 + 重启逻辑 + 重试计数器
|
2026-06-08 10:33:36 +08:00
|
|
|
|
├── server_status_detector.rb # TCP 端口检测模块
|
|
|
|
|
|
├── systemd_notifier.rb # systemd watchdog 心跳发送
|
2026-06-08 11:35:51 +08:00
|
|
|
|
├── config.rb # 可调参数配置
|
|
|
|
|
|
└── updates.rb # 更新检查模块
|
2026-06-07 19:35:17 +08:00
|
|
|
|
|
|
|
|
|
|
minesentinel.service # systemd 单元文件(项目源码中)
|
2026-06-07 11:05:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
2026-06-07 19:35:17 +08:00
|
|
|
|
> `uni_log_path.rb` 已废弃。
|
|
|
|
|
|
|
2026-06-07 11:05:26 +08:00
|
|
|
|
## 部署步骤
|
|
|
|
|
|
|
|
|
|
|
|
### 1. 创建目录并复制文件
|
|
|
|
|
|
|
|
|
|
|
|
```bash
|
2026-06-07 19:35:17 +08:00
|
|
|
|
mkdir -p /opt/minecraft/sentinel
|
2026-06-08 11:35:51 +08:00
|
|
|
|
cp main.rb method_executor.rb server_status_detector.rb systemd_notifier.rb config.rb updates.rb /opt/minecraft/sentinel/
|
2026-06-07 11:05:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### 2. 确认 Minecraft systemd 服务存在
|
|
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
systemctl status minecraft
|
|
|
|
|
|
```
|
|
|
|
|
|
|
2026-06-07 19:35:17 +08:00
|
|
|
|
如果服务名不是 `minecraft`,修改 `method_executor.rb` 中所有 `systemctl xxx minecraft` 为实际服务名(共 3 处:restart / kill / start),同时修改 `minesentinel.service` 中的 `Before=minecraft.service` 和 `Wants=minecraft.service`。
|
2026-06-07 11:05:26 +08:00
|
|
|
|
|
|
|
|
|
|
### 3. 手动测试
|
|
|
|
|
|
|
|
|
|
|
|
```bash
|
2026-06-07 19:35:17 +08:00
|
|
|
|
cd /opt/minecraft/sentinel
|
2026-06-07 11:05:26 +08:00
|
|
|
|
ruby main.rb
|
2026-06-07 19:35:17 +08:00
|
|
|
|
# 调试模式:LOG_LEVEL=debug ruby main.rb
|
2026-06-07 11:05:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
2026-06-07 19:35:17 +08:00
|
|
|
|
### 4. 注册 systemd 服务
|
2026-06-07 11:05:26 +08:00
|
|
|
|
|
2026-06-07 19:35:17 +08:00
|
|
|
|
项目源码中已提供 `minesentinel.service`,直接复制:
|
2026-06-07 11:05:26 +08:00
|
|
|
|
|
|
|
|
|
|
```bash
|
2026-06-07 19:35:17 +08:00
|
|
|
|
cp minesentinel.service /etc/systemd/system/
|
2026-06-07 11:05:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
2026-06-07 19:35:17 +08:00
|
|
|
|
单元文件内容(`minesentinel.service`):
|
2026-06-07 11:05:26 +08:00
|
|
|
|
|
|
|
|
|
|
```ini
|
|
|
|
|
|
[Unit]
|
2026-06-07 19:35:17 +08:00
|
|
|
|
Description=MineSentinel Minecraft Server Monitor
|
2026-06-07 11:05:26 +08:00
|
|
|
|
After=network.target
|
2026-06-07 19:35:17 +08:00
|
|
|
|
Before=minecraft.service
|
|
|
|
|
|
Wants=minecraft.service
|
2026-06-07 11:05:26 +08:00
|
|
|
|
|
|
|
|
|
|
[Service]
|
|
|
|
|
|
Type=simple
|
2026-06-07 19:35:17 +08:00
|
|
|
|
WorkingDirectory=/opt/minecraft/sentinel
|
|
|
|
|
|
ExecStart=/usr/bin/ruby /opt/minecraft/sentinel/main.rb
|
2026-06-07 11:05:26 +08:00
|
|
|
|
Restart=always
|
2026-06-07 19:35:17 +08:00
|
|
|
|
RestartSec=10
|
2026-06-07 11:05:26 +08:00
|
|
|
|
StandardOutput=journal
|
|
|
|
|
|
StandardError=journal
|
2026-06-07 19:35:17 +08:00
|
|
|
|
#Environment=LOG_LEVEL=debug
|
|
|
|
|
|
#Environment=RUBY_DEBUG=1
|
|
|
|
|
|
WatchdogSec=210
|
2026-06-07 11:05:26 +08:00
|
|
|
|
|
|
|
|
|
|
[Install]
|
|
|
|
|
|
WantedBy=multi-user.target
|
|
|
|
|
|
```
|
|
|
|
|
|
|
2026-06-07 19:35:17 +08:00
|
|
|
|
| 参数 | 说明 |
|
|
|
|
|
|
|------|------|
|
|
|
|
|
|
| `Before=minecraft.service` | MineSentinel 先于 MC 启动,消除守护盲区 |
|
|
|
|
|
|
| `Wants=minecraft.service` | 软依赖 MC 服务,MC 启动失败不影响 MineSentinel |
|
|
|
|
|
|
| `RestartSec=10` | MineSentinel 自身崩溃后 10s 重启 |
|
|
|
|
|
|
| `WatchdogSec=210` | 必须大于冷却期(200s),防止 watchdog 误杀 |
|
|
|
|
|
|
|
|
|
|
|
|
启动顺序:
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
开机 → network.target → MineSentinel → (300s 宽限期) → minecraft 启动 → 端口就绪
|
|
|
|
|
|
```
|
2026-06-07 11:05:26 +08:00
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
systemctl daemon-reload
|
2026-06-07 19:35:17 +08:00
|
|
|
|
systemctl enable minesentinel # 同时也会触发 minecraft(Wants=)
|
2026-06-07 11:05:26 +08:00
|
|
|
|
systemctl start minesentinel
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### 5. 查看日志
|
|
|
|
|
|
|
|
|
|
|
|
```bash
|
2026-06-07 19:35:17 +08:00
|
|
|
|
journalctl -u minesentinel -f # 实时
|
|
|
|
|
|
journalctl -u minesentinel -u minecraft -f # 双服务合并
|
|
|
|
|
|
```
|
2026-06-07 11:05:26 +08:00
|
|
|
|
|
2026-06-07 19:35:17 +08:00
|
|
|
|
## 工作流程
|
2026-06-07 11:05:26 +08:00
|
|
|
|
|
|
|
|
|
|
```
|
2026-06-07 19:35:17 +08:00
|
|
|
|
MineSentinel 启动:
|
|
|
|
|
|
├─ 进入初始宽限期(300s),等待 MC 端口就绪
|
|
|
|
|
|
└─ 宽限期结束 → 进入正常监控循环
|
2026-06-07 11:05:26 +08:00
|
|
|
|
|
2026-06-07 19:35:17 +08:00
|
|
|
|
每 1.5 秒循环:
|
|
|
|
|
|
├─ TCP 连接 127.0.0.1:25565
|
|
|
|
|
|
├─ 端口开放 → 继续循环
|
|
|
|
|
|
├─ 端口超时 → 仅记录日志(不重启,可能是网络抖动)
|
|
|
|
|
|
└─ 端口关闭 → 触发重启 → 冷却 200 秒等待启动
|
|
|
|
|
|
└─ 连续失败 10 次 → 进入空闲等待,直到手动恢复
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## 可调参数
|
|
|
|
|
|
|
|
|
|
|
|
| 参数 | 文件 | 行 | 默认值 |
|
|
|
|
|
|
|------|------|-----|--------|
|
2026-06-08 10:33:36 +08:00
|
|
|
|
| 初始宽限期 | `config.rb` | 1 | `300` s |
|
|
|
|
|
|
| 冷却期 | `config.rb` | 3 | `200` s |
|
|
|
|
|
|
| 尝试次数 | `config.rb` | 2 | `3` 次 |
|
2026-06-08 10:37:12 +08:00
|
|
|
|
| 检测间隔 | `config.rb` | 4 | `1.5` s |
|
2026-06-07 19:35:17 +08:00
|
|
|
|
| systemctl 超时 | `method_executor.rb` | 17 | `30` s |
|
|
|
|
|
|
| 最大重试 | `method_executor.rb` | 23 | `10` 次 |
|
|
|
|
|
|
| 检测端口 | `server_status_detector.rb` | 60 | `25565` |
|
|
|
|
|
|
| TCP 超时 | `server_status_detector.rb` | 23 | `10` s |
|
2026-06-07 11:05:26 +08:00
|
|
|
|
|
2026-06-07 19:35:17 +08:00
|
|
|
|
## 日志级别
|
2026-06-07 11:05:26 +08:00
|
|
|
|
|
2026-06-07 19:35:17 +08:00
|
|
|
|
`Environment=LOG_LEVEL=xxx`,默认 `debug`(未设置时),生产推荐 `info`。
|
|
|
|
|
|
|
|
|
|
|
|
| 级别 | 内容 |
|
2026-06-07 11:05:26 +08:00
|
|
|
|
|------|------|
|
2026-06-07 19:35:17 +08:00
|
|
|
|
| `debug` | 每次巡检、TCP 响应时间、systemctl PID |
|
|
|
|
|
|
| `info` | 启动/宽限期/重启/恢复/冷却期 |
|
|
|
|
|
|
| `warn` | 端口超时/关闭、强制终止 |
|
|
|
|
|
|
| `error` | 重启失败、重试耗尽 |
|
2026-06-07 11:05:26 +08:00
|
|
|
|
|
2026-06-07 19:35:17 +08:00
|
|
|
|
## 跨场景适配指南
|
2026-06-07 11:05:26 +08:00
|
|
|
|
|
2026-06-07 19:35:17 +08:00
|
|
|
|
### 无 systemd(Docker / screen / 手动启动)
|
2026-06-07 11:05:26 +08:00
|
|
|
|
|
2026-06-07 19:35:17 +08:00
|
|
|
|
核心:替换 `method_executor.rb` 第 86 行的重启命令,并移除第 80-83 行 root 权限检查。
|
|
|
|
|
|
|
|
|
|
|
|
**Docker:**
|
|
|
|
|
|
```ruby
|
|
|
|
|
|
pid = spawn "docker restart mc-server"
|
2026-06-07 11:05:26 +08:00
|
|
|
|
```
|
2026-06-07 19:35:17 +08:00
|
|
|
|
|
|
|
|
|
|
**screen:**
|
|
|
|
|
|
```ruby
|
|
|
|
|
|
system "screen -S mc -X quit"
|
|
|
|
|
|
sleep 2
|
|
|
|
|
|
system "screen -dmS mc java -Xmx4G -jar server.jar nogui"
|
2026-06-07 11:05:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
2026-06-07 19:35:17 +08:00
|
|
|
|
**supervisor:**
|
|
|
|
|
|
```ruby
|
|
|
|
|
|
pid = spawn "supervisorctl restart minecraft"
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### Windows 环境
|
|
|
|
|
|
|
|
|
|
|
|
核心逻辑跨平台,仅需适配重启方式。`method_executor.rb` 修改:
|
|
|
|
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
|
|
# 删除第 80-83 行 root 权限检查(Windows 无 Process.euid)
|
|
|
|
|
|
|
|
|
|
|
|
# 第 86 行替换为(直接调 Java 或 bat):
|
|
|
|
|
|
pid = spawn "cmd /c start /B java -Xmx4G -jar C:\\mc\\server.jar nogui"
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
日志输出到文件(Windows 无 journal)—— 在 `method_executor.rb` 第 26 行改为:
|
|
|
|
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
|
|
@@logger = Logger.new("C:\\mc\\sentinel.log", "daily")
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
以管理员身份运行终端,`ruby main.rb` 即可。
|
|
|
|
|
|
|
|
|
|
|
|
### 监控其他服务
|
|
|
|
|
|
|
|
|
|
|
|
`server_status_detector.rb` 的 `test_port` 是通用 TCP 检测,改端口和 IP 即可监控任意服务:
|
|
|
|
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
|
|
# server_status_detector.rb 第 60 行
|
|
|
|
|
|
res = test_port('192.168.1.100', 3306) # MySQL
|
|
|
|
|
|
res = test_port('127.0.0.1', 80) # Nginx
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
配合修改 `method_executor.rb` 中的重启命令即可适配。
|
|
|
|
|
|
|
2026-06-07 11:05:26 +08:00
|
|
|
|
## 常见问题
|
|
|
|
|
|
|
2026-06-07 19:35:17 +08:00
|
|
|
|
**Q: 报 `Errno::EPERM`?** A: 需 root 权限或 `sudo ruby main.rb`。
|
|
|
|
|
|
|
|
|
|
|
|
**Q: 端口一直超时但不重启?** A: 设计如此。`:timeout` 只记录不重启,仅 `:closed` 触发重启。
|
|
|
|
|
|
|
|
|
|
|
|
**Q: 启动时 MineSentinel 反复重启 minecraft?** A: 检查 `INITIAL_GRACE` 是否小于 MC 实际启动时间,适当调大。
|
2026-06-07 11:05:26 +08:00
|
|
|
|
|
2026-06-07 19:35:17 +08:00
|
|
|
|
**Q: 如何修改 Minecraft 服务名?** A: 编辑 `method_executor.rb`,搜索 `systemctl xxx minecraft`(3 处),替换 `minecraft` 为实际服务名。同时修改 `minesentinel.service` 中的 `Before=` 和 `Wants=`。
|
2026-06-07 11:05:26 +08:00
|
|
|
|
|
2026-06-07 19:35:17 +08:00
|
|
|
|
## 许可证
|
2026-06-07 11:05:26 +08:00
|
|
|
|
|
2026-06-07 19:35:17 +08:00
|
|
|
|
MineSentinel 源码采用 [木兰宽松许可证 v2](http://license.coscl.org.cn/MulanPubL-2.0)。品牌标识(Logo / 项目名称)保留所有权利,未经许可不得用于衍生项目。
|