From 266f29853e1ad101c68370223ca2e3896f94ad26 Mon Sep 17 00:00:00 2001 From: Leon Date: Mon, 7 Sep 2026 11:08:20 +0800 Subject: [PATCH] Add autostart watchdog task --- setup_autostart.bat | 25 +++++++++------ setup_autostart.ps1 | 75 +++++++++++++++++++++++++++++++++++++++++++++ watchdog.bat | 17 ++++++++++ 3 files changed, 108 insertions(+), 9 deletions(-) create mode 100644 setup_autostart.ps1 create mode 100644 watchdog.bat diff --git a/setup_autostart.bat b/setup_autostart.bat index cca88ff..7330e55 100644 --- a/setup_autostart.bat +++ b/setup_autostart.bat @@ -4,9 +4,12 @@ setlocal cd /d "%~dp0" set "TASK_NAME=DocuTranslate" +set "WATCHDOG_TASK_NAME=DocuTranslateWatchdog" set "PROJECT_DIR=%~dp0" set "RUN_BAT=%PROJECT_DIR%run.bat" set "RUNNER_BAT=%PROJECT_DIR%autostart_runner.bat" +set "WATCHDOG_BAT=%PROJECT_DIR%watchdog.bat" +set "SETUP_PS1=%PROJECT_DIR%setup_autostart.ps1" set "LOG_DIR=%PROJECT_DIR%logs" set "LOG_FILE=%LOG_DIR%\autostart.log" @@ -22,16 +25,19 @@ if not exist "%RUNNER_BAT%" ( exit /b 1 ) -if not exist "%LOG_DIR%" mkdir "%LOG_DIR%" +if not exist "%WATCHDOG_BAT%" ( + echo [ERROR] Cannot find "%WATCHDOG_BAT%". + pause + exit /b 1 +) -REM Create a startup task. Run this file as Administrator. -REM The task runs as SYSTEM, so it can start after reboot before desktop login. -powershell -NoProfile -ExecutionPolicy Bypass -Command ^ - "$Action = New-ScheduledTaskAction -Execute 'cmd.exe' -Argument '/c ""%RUNNER_BAT%""';" ^ - "$Trigger = New-ScheduledTaskTrigger -AtStartup;" ^ - "$Principal = New-ScheduledTaskPrincipal -UserId 'SYSTEM' -RunLevel Highest;" ^ - "$Settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -MultipleInstances IgnoreNew;" ^ - "Register-ScheduledTask -TaskName '%TASK_NAME%' -Action $Action -Trigger $Trigger -Principal $Principal -Settings $Settings -Force | Out-Null" +if not exist "%SETUP_PS1%" ( + echo [ERROR] Cannot find "%SETUP_PS1%". + pause + exit /b 1 +) + +powershell -NoProfile -ExecutionPolicy Bypass -File "%SETUP_PS1%" if not "%ERRORLEVEL%"=="0" ( echo. @@ -42,6 +48,7 @@ if not "%ERRORLEVEL%"=="0" ( echo. echo [OK] Scheduled task "%TASK_NAME%" has been created. +echo [OK] Scheduled task "%WATCHDOG_TASK_NAME%" has been created. echo [OK] It will run "%RUNNER_BAT%" at system startup. echo [OK] Log file: "%LOG_FILE%" echo. diff --git a/setup_autostart.ps1 b/setup_autostart.ps1 new file mode 100644 index 0000000..fe854ad --- /dev/null +++ b/setup_autostart.ps1 @@ -0,0 +1,75 @@ +$ErrorActionPreference = "Stop" + +$ProjectDir = Split-Path -Parent $MyInvocation.MyCommand.Path +$TaskName = "DocuTranslate" +$WatchdogTaskName = "DocuTranslateWatchdog" +$RunnerBat = Join-Path $ProjectDir "autostart_runner.bat" +$WatchdogBat = Join-Path $ProjectDir "watchdog.bat" +$LogDir = Join-Path $ProjectDir "logs" +$LogFile = Join-Path $LogDir "autostart.log" + +if (!(Test-Path -LiteralPath $RunnerBat)) { + throw "Cannot find $RunnerBat" +} + +if (!(Test-Path -LiteralPath $WatchdogBat)) { + throw "Cannot find $WatchdogBat" +} + +if (!(Test-Path -LiteralPath $LogDir)) { + New-Item -ItemType Directory -Path $LogDir | Out-Null +} + +$Principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -RunLevel Highest +$StartupAction = New-ScheduledTaskAction -Execute "cmd.exe" -Argument "/c `"$RunnerBat`"" +$StartupTrigger = New-ScheduledTaskTrigger -AtStartup +try { + $StartupSettings = New-ScheduledTaskSettingsSet ` + -StartWhenAvailable ` + -MultipleInstances IgnoreNew ` + -RestartCount 3 ` + -RestartInterval (New-TimeSpan -Minutes 1) +} catch { + $StartupSettings = New-ScheduledTaskSettingsSet ` + -StartWhenAvailable ` + -MultipleInstances IgnoreNew +} + +Register-ScheduledTask ` + -TaskName $TaskName ` + -Action $StartupAction ` + -Trigger $StartupTrigger ` + -Principal $Principal ` + -Settings $StartupSettings ` + -Force | Out-Null + +$WatchdogAction = New-ScheduledTaskAction -Execute "cmd.exe" -Argument "/c `"$WatchdogBat`"" +$WatchdogTrigger = New-ScheduledTaskTrigger ` + -Once ` + -At (Get-Date).AddMinutes(1) ` + -RepetitionInterval (New-TimeSpan -Minutes 5) ` + -RepetitionDuration (New-TimeSpan -Days 3650) +$WatchdogSettings = New-ScheduledTaskSettingsSet ` + -StartWhenAvailable ` + -MultipleInstances IgnoreNew + +Register-ScheduledTask ` + -TaskName $WatchdogTaskName ` + -Action $WatchdogAction ` + -Trigger $WatchdogTrigger ` + -Principal $Principal ` + -Settings $WatchdogSettings ` + -Force | Out-Null + +Write-Host "[OK] Scheduled task `"$TaskName`" has been created." +Write-Host "[OK] Scheduled task `"$WatchdogTaskName`" has been created." +Write-Host "[OK] Startup runner: `"$RunnerBat`"" +Write-Host "[OK] Watchdog: `"$WatchdogBat`"" +Write-Host "[OK] Log file: `"$LogFile`"" +Write-Host "" +Write-Host "Verify:" +Write-Host "schtasks /Query /TN `"$TaskName`" /V /FO LIST" +Write-Host "schtasks /Query /TN `"$WatchdogTaskName`" /V /FO LIST" +Write-Host "" +Write-Host "Start now:" +Write-Host "schtasks /Run /TN `"$TaskName`"" diff --git a/watchdog.bat b/watchdog.bat new file mode 100644 index 0000000..9708f18 --- /dev/null +++ b/watchdog.bat @@ -0,0 +1,17 @@ +@echo off +setlocal + +set "TASK_NAME=DocuTranslate" +set "HEALTH_URL=http://127.0.0.1:8010/service/meta" +set "LOG_DIR=%~dp0logs" +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 }" + +exit /b 0