乐于分享
好东西不私藏

AI:用Monitor提升Hermes的可用性(附代码)

AI:用Monitor提升Hermes的可用性(附代码)

   一键复制  
BREAKING2026.05

如何建构一个Hermes Agent Monitor?

Hermes Agent 通过 Monitor 监控脚本保持微信机器人后台常驻运行。

PART01

设计目标

  • •每 5 分钟自动检测 Hermes Gateway 是否在运行
  • •若 Hermes 异常退出,自动重启
  • •防止多实例运行(使用 monitor.lock 锁文件)
  • •日志记录至 gateway-monitor.log
PART02

启动

💡工作原理

以 window_style=0(隐藏窗口)启动 Pythonw.exe,实现 Monitor 在后台静默运行,而不影响用户操作

方式一:双击启动器(推荐)

双击 hermes-gateway-monitor-launcher.bat

启动后窗口会停留并显示最近日志,按任意键关闭窗口,Monitor 在后台继续运行。

方式二:任务计划程序(开机自启)

已配置开机自启动,无需手动操作。

主要代码如下:

@echo offchcp 65001 > nultitle Hermes Gateway Monitor LauncherREM ========================================REM  Hermes Gateway Monitor LauncherREM  Starts Hermes Gateway Monitor silently,REM  then waits and shows startup status.REM  Window stays open until user presses a key.REM ========================================set "HERMES_ROOT=\hermes-agent"set "MONITOR_SCRIPT=\hermes-gateway-monitor.py"set "VENV_PYTHON=%HERMES_ROOT%\venv\Scripts\pythonw.exe"set "LOG_DIR=\logs"set "LAUNCHER_LOG=%LOG_DIR%\launcher.log"set "HELPER_SCRIPT=%~dp0launcher-helper.vbs"set "MONITOR_LOG=%LOG_DIR%\gateway-monitor.log"set "TMP_FILE=%TEMP%\hermes_launch_status.tmp"if not exist "%LOG_DIR%" mkdir "%LOG_DIR%" 2>nulecho ========================================echo   Hermes Gateway Monitor Launcherecho ========================================echo.REM Log to file (append)call :log "========== Launcher Start =========="call :log "Time: %DATE% %TIME%"REM Check Python venvcall :log "Checking Python venv: %VENV_PYTHON%"if not exist "%VENV_PYTHON%" (    call :log "ERROR: Python venv not found"    echo [ERROR] Python venv not found: %VENV_PYTHON%    echo.    pause    exit /b 1)call :log "Python venv OK"echo [OK] Python venv found.REM Check monitor scriptcall :log "Checking monitor script: %MONITOR_SCRIPT%"if not exist "%MONITOR_SCRIPT%" (    call :log "ERROR: Monitor script not found"    echo [ERROR] Monitor script not found: %MONITOR_SCRIPT%    echo.    pause    exit /b 1)call :log "Monitor script OK"echo [OK] Monitor script found.REM Check VBScript helperif not exist "%HELPER_SCRIPT%" (    call :log "ERROR: Helper script not found: %HELPER_SCRIPT%"    echo [ERROR] Helper script not found: %HELPER_SCRIPT%    echo.    pause    exit /b 1)call :log "Helper script OK"echo [OK] VBScript helper found.REM Change to Hermes rootcd /d "%HERMES_ROOT%"call :log "CWD: %CD%"REM Start monitor via VBScript (runs silently, no window)call :log "Starting monitor via VBScript..."echo.echo [INFO] Starting Hermes Gateway Monitor...echo [INFO] Waiting for monitor to initialize (5s)...echo.cscript //nologo "%HELPER_SCRIPT%" "%VENV_PYTHON%" "%MONITOR_SCRIPT%"call :log "Monitor start command issued."REM Wait for monitor to write its first log entriestimeout /t 5 > nulREM ========================================REM Display monitor startup status from logREM ========================================echo.echo ========================================echo   Monitor Startup Statusecho ========================================if exist "%MONITOR_LOG%" (    echo.    echo [LOG] Last entries from gateway-monitor.log:    echo ----------------------------------------    REM Use temp file to safely pass UTF-8 content to console    powershell -NoProfile -Command "Get-Content '%MONITOR_LOG%' -Tail 12 -Encoding UTF8" > "%TMP_FILE%" 2>nul    for /f "delims=" %%i in ("%TMP_FILE%"do echo   %%i    del "%TMP_FILE%" > nul 2>&1    echo ----------------------------------------else (    echo.    echo [WARN] Monitor log file not found: %MONITOR_LOG%    echo [WARN] Monitor may have failed to start.)echo.echo ========================================echo   Next Stepsecho ========================================echo  - Monitor runs silently in background (checks every 5 min)echo  - Check status anytime:  hermes-monitor-status.batecho  - Stop monitor:          hermes-monitor-stop.batecho  - Monitor log:           \logs\gateway-monitor.logecho.echo Press any key to close this window...pause > nulexit /b 0:logecho [%DATE% %TIME%] [Launcher] %~1 >> "%LAUNCHER_LOG%" 2>nulgoto :eof
PART03

查看运行状态

显示以下四项状态:

  • •Monitor 进程:[OK] Monitor is running
  • •Hermes 状态文件:[OK] State file exists
  • •Hermes 锁文件:[OK] Lock file exists
  • •日志文件:[OK] Log file exists
💡状态检查原理

Monitor 运行时会在用户目录下创建 monitor.lock 文件存放自身 PID,退出时自动删除。通过检查该文件存在性即可判断运行状态

也可以直接查看日志:notepad gateway-monitor.log

正常状态日志示例:✅ Hermes 运行正常 (PID: 19944, WeChat: connected)

主要代码如下:

@echo offchcp 65001 > nultitle Hermes Monitor Statusset "MONITOR_LOCK=%USERPROFILE%\.hermes\monitor.lock"set "GATEWAY_LOCK=%USERPROFILE%\.hermes\gateway.lock"set "GATEWAY_STATE=%USERPROFILE%\.hermes\gateway_state.json"set "LOG_FILE=\logs\gateway-monitor.log"echo ========================================echo   Hermes Gateway Monitor Statusecho ========================================echo.echo [1] Monitor Status:if exist "%MONITOR_LOCK%" (    echo   [OK] Monitor is runningelse (    echo   [X] Monitor is not running)echo.echo [2] Hermes Gateway Status:if exist "%GATEWAY_STATE%" (    echo   [OK] State file exists    if exist "%GATEWAY_LOCK%" (        echo   [OK] Lock file exists    ) else (        echo   [!] Lock file missing    )else (    echo   [X] State file missing)echo.echo [3] Log File:if exist "%LOG_FILE%" (    echo   [OK] Log file existselse (    echo   [!] Log file missing)echo.echo [4] Python venv:if exist "\hermes-agent\venv\Scripts\python.exe" (    echo   [OK] venv existselse (    echo   [X] venv missing)echo.echo ========================================echo.pauseexit /b 0
PART04

停止

方法一:信号文件(优雅停止)

创建停止信号文件,Monitor 会在下一个检查周期自动停止 Hermes 并退出

🤖 创建停止信号

echo. > stop_hermes.signal

优雅停止

Monitor 每 5 分钟检查信号文件是否存在,若检测到该文件,则停止 Hermes 后自动退出,无需强制杀进程

方法二:双击停止脚本(推荐)

双击 hermes-monitor-stop.bat

🤖 停止脚本核心代码

:: 通过命令行匹配找到并终止 Hermes 进程

wmic process where “commandline like ‘%%hermes_cli%%'” call terminate

:: 清理 Hermes 相关锁文件

if exist “gateway.lock” del /f /q “gateway.lock”

:: 终止 Monitor 进程

wmic process where “commandline like ‘%%hermes-gateway-monitor.py%%'” call terminate

💡进程终止原理

使用 WMIC 通过命令行关键字匹配进程,避免依赖 PID 文件(PID 可能已失效),实现可靠进程终止

方法三:强制终止

若以上方法无效,手动杀进程:

🤖 强制终止命令

taskkill /F /IM pythonw.exe

del monitor.lock

主要代码如下:

@echo offchcp 65001 > nultitle Hermes Monitor Stopset "MONITOR_LOCK=%USERPROFILE%\.hermes\monitor.lock"set "GATEWAY_LOCK=%USERPROFILE%\.hermes\gateway.lock"set "GATEWAY_STATE=%USERPROFILE%\.hermes\gateway_state.json"set "STOP_SIGNAL=%USERPROFILE%\.hermes\stop_hermes.signal"echo ========================================echo   Hermes Monitor Stopperecho ========================================echo.REM --- 1. Kill Hermes Gateway (by command line match) ---echo [1] Stopping Hermes Gateway...wmic process where "commandline like '%%hermes_cli%%' and commandline like '%%gateway%%'" call terminate >nul 2>&1if errorlevel 1 (    echo     [SKIP] No Hermes process foundelse (    echo     [OK] Hermes process terminated)timeout /t 2 /nobreak > nulecho.REM --- 2. Remove Hermes lock files ---if exist "%GATEWAY_LOCK%" (    del /f /q "%GATEWAY_LOCK%" >nul 2>&1    echo [2] Removed gateway.lock)if exist "%GATEWAY_STATE%" (    del /f /q "%GATEWAY_STATE%" >nul 2>&1    echo [3] Removed gateway_state.json)echo.REM --- 3. Kill Monitor (by command line match, no PID read) ---echo [4] Stopping Monitor...wmic process where "commandline like '%%hermes-gateway-monitor.py%%'" call terminate >nul 2>&1if errorlevel 1 (    echo     [SKIP] Monitor not runningelse (    echo     [OK] Monitor process terminated)echo.REM --- 4. Remove Monitor lock & signal files ---if exist "%MONITOR_LOCK%" (    del /f /q "%MONITOR_LOCK%" >nul 2>&1    echo [5] Removed monitor.lock)if exist "%STOP_SIGNAL%" (    del /f /q "%STOP_SIGNAL%" >nul 2>&1    echo [6] Removed stop signal file)echo.echo ========================================echo   All stopped and cleanecho ========================================echo.pauseexit /b 0
PART05

常见故障排查

Monitor 启动后自动退出

原因:monitor.lock 残留(上次异常退出时未清理)

解决:

  • •删除残留锁文件:del monitor.lock
  • •重新运行 hermes-gateway-monitor-launcher.bat

Hermes 启动失败

排查步骤:

  • •查看 hermes-startup.log 获取 Hermes 启动错误详情
  • •常见原因:微信 iLink 未启动、Kimi API 限流、配置文件错误

Monitor 无反应(日志不更新)

原因:Monitor 进程卡死或 Python 环境损坏

解决:

🤖 重启 Monitor

taskkill /F /IM pythonw.exe

hermes-gateway-monitor-launcher.bat

如果这篇文章对你有帮助

点个赞、收藏起来,或者转发给需要的朋友

点赞
收藏
转发