Add autostart watchdog task
This commit is contained in:
+16
-9
@@ -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.
|
||||
|
||||
@@ -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`""
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user