Harden Windows service supervision

This commit is contained in:
2026-09-07 11:19:45 +08:00
parent 266f29853e
commit 44ce5e655d
9 changed files with 199 additions and 9 deletions
+15 -4
View File
@@ -9,9 +9,20 @@ set "LOG_FILE=%LOG_DIR%\watchdog.log"
if not exist "%LOG_DIR%" mkdir "%LOG_DIR%"
powershell -NoProfile -ExecutionPolicy Bypass -Command ^
"$ErrorActionPreference = 'SilentlyContinue';" ^
"$ok = $false;" ^
"try { $r = Invoke-WebRequest -UseBasicParsing -Uri '%HEALTH_URL%' -TimeoutSec 5; $ok = ($r.StatusCode -eq 200) } catch { $ok = $false };" ^
"if (-not $ok) { Add-Content -Path '%LOG_FILE%' -Value ('[' + (Get-Date) + '] Health check failed, restarting %TASK_NAME%...'); schtasks /End /TN '%TASK_NAME%' | Out-Null; Start-Sleep -Seconds 2; schtasks /Run /TN '%TASK_NAME%' | Out-Null }"
"$ErrorActionPreference = 'Stop'; try { $r = Invoke-WebRequest -UseBasicParsing -Uri '%HEALTH_URL%' -TimeoutSec 8; if ($r.StatusCode -eq 200) { exit 0 } } catch {}; exit 1"
if "%ERRORLEVEL%"=="0" exit /b 0
timeout /t 10 /nobreak >nul
powershell -NoProfile -ExecutionPolicy Bypass -Command ^
"$ErrorActionPreference = 'Stop'; try { $r = Invoke-WebRequest -UseBasicParsing -Uri '%HEALTH_URL%' -TimeoutSec 8; if ($r.StatusCode -eq 200) { exit 0 } } catch {}; exit 1"
if "%ERRORLEVEL%"=="0" exit /b 0
echo [%DATE% %TIME%] Two health checks failed. Restarting %TASK_NAME%...>> "%LOG_FILE%"
schtasks /End /TN "%TASK_NAME%" >> "%LOG_FILE%" 2>&1
timeout /t 2 /nobreak >nul
schtasks /Run /TN "%TASK_NAME%" >> "%LOG_FILE%" 2>&1
exit /b 0