zhizhijie
2026-10-06 bee9e18c3af284ff432a4472f1d409f5a6dc09a7
deploy-templates/backend/start-backend-service.cmd
New file
@@ -0,0 +1,49 @@
@echo off
rem ============================================================
rem  Non-interactive launcher for Task Scheduler / boot auto-start.
rem  Differences from start-backend.cmd (which is meant for a human):
rem    - no "pause" at the end (nobody can press a key during boot)
rem    - all output goes to app.log next to this file
rem    - resolves java via JAVA_HOME first, then PATH
rem    - exits 0 immediately if port 8090 is already listening
rem  ASCII-only on purpose: cmd.exe reads this file with the OEM
rem  code page, so non-ASCII text here would be garbled.
rem  Registered by install-autostart.ps1; run under the account that
rem  owns the deployment folder (SYSTEM by default).
rem ============================================================
setlocal
cd /d "%~dp0"
set "APP_JAR=traffic-audit-server.jar"
set "LOG=%~dp0app.log"
netstat -ano | findstr /C:":8090" | findstr /I "LISTENING" >nul 2>nul
if not errorlevel 1 (
  echo [%date% %time%] port 8090 is already listening; nothing to do. >> "%LOG%"
  exit /b 0
)
if not exist "%~dp0%APP_JAR%" (
  echo [%date% %time%] ERROR: %APP_JAR% not found in "%CD%". >> "%LOG%"
  exit /b 1
)
set "JAVA_EXE="
if defined JAVA_HOME if exist "%JAVA_HOME%\bin\java.exe" set "JAVA_EXE=%JAVA_HOME%\bin\java.exe"
if not defined JAVA_EXE for %%J in (java.exe) do set "JAVA_EXE=%%~$PATH:J"
if not defined JAVA_EXE (
  echo [%date% %time%] ERROR: java.exe not found. Install JDK 8 or set JAVA_HOME / PATH. >> "%LOG%"
  exit /b 1
)
if not exist "%~dp0application.yml" (
  echo [%date% %time%] WARN: application.yml is missing next to the jar. >> "%LOG%"
  echo [%date% %time%]       The DB password placeholder will resolve to empty and startup will fail with >> "%LOG%"
  echo [%date% %time%]       "Access denied ... (using password: NO)". >> "%LOG%"
)
echo [%date% %time%] starting backend: "%JAVA_EXE%" -jar "%APP_JAR%"  (cwd "%CD%") >> "%LOG%"
"%JAVA_EXE%" -jar "%~dp0%APP_JAR%" >> "%LOG%" 2>&1
set "RC=%ERRORLEVEL%"
echo [%date% %time%] backend exited with code %RC%. >> "%LOG%"
endlocal & exit /b %RC%