102 lines
2.4 KiB
Batchfile
102 lines
2.4 KiB
Batchfile
@echo off
|
|
setlocal
|
|
chcp 65001 >nul 2>&1
|
|
|
|
set "ROOT=%~dp0"
|
|
if exist "%ROOT%scripts\load-env.bat" call "%ROOT%scripts\load-env.bat" "%ROOT%config\service.env"
|
|
set "LAUNCHER_DIR=%ROOT%temp\launcher"
|
|
if "%API_PORT%"=="" set "API_PORT=8910"
|
|
|
|
echo ========================================
|
|
echo Map Asset Gateway Stopper
|
|
echo ========================================
|
|
echo.
|
|
|
|
call :stop_pid_file "Map Asset Gateway" "%LAUNCHER_DIR%\api.pid"
|
|
if not "%STOP_RESULT%"=="stopped" call :stop_port "Map Asset Gateway" "%API_PORT%"
|
|
call :stop_window_title "Map Asset Gateway"
|
|
|
|
echo.
|
|
echo Finished.
|
|
if "%NO_PAUSE%"=="" pause
|
|
exit /b 0
|
|
|
|
:stop_pid_file
|
|
set "STOP_RESULT=missing"
|
|
set "SERVICE_NAME=%~1"
|
|
set "PID_FILE=%~2"
|
|
if not exist "%PID_FILE%" (
|
|
echo [SKIP] %SERVICE_NAME% not running
|
|
set "SERVICE_NAME="
|
|
set "PID_FILE="
|
|
exit /b 0
|
|
)
|
|
|
|
set "TARGET_PID="
|
|
for /f "usebackq delims=" %%I in ("%PID_FILE%") do (
|
|
set "TARGET_PID=%%I"
|
|
goto :pid_loaded
|
|
)
|
|
:pid_loaded
|
|
if not defined TARGET_PID (
|
|
echo [SKIP] %SERVICE_NAME% not running
|
|
del /q "%PID_FILE%" >nul 2>&1
|
|
set "SERVICE_NAME="
|
|
set "PID_FILE="
|
|
set "TARGET_PID="
|
|
exit /b 0
|
|
)
|
|
|
|
call :kill_pid "%TARGET_PID%"
|
|
if not "%KILL_OK%"=="1" (
|
|
echo [SKIP] %SERVICE_NAME% not running
|
|
) else (
|
|
echo [OK] Stopped %SERVICE_NAME% PID=%TARGET_PID%
|
|
set "STOP_RESULT=stopped"
|
|
)
|
|
|
|
del /q "%PID_FILE%" >nul 2>&1
|
|
set "SERVICE_NAME="
|
|
set "PID_FILE="
|
|
set "TARGET_PID="
|
|
exit /b 0
|
|
|
|
:stop_port
|
|
set "SERVICE_NAME=%~1"
|
|
set "TARGET_PORT=%~2"
|
|
set "FOUND_PID="
|
|
for /f "tokens=5" %%I in ('netstat -ano -p TCP ^| findstr /R /C:":%TARGET_PORT% .*LISTENING"') do set "FOUND_PID=%%I"
|
|
if not defined FOUND_PID (
|
|
set "SERVICE_NAME="
|
|
set "TARGET_PORT="
|
|
exit /b 0
|
|
)
|
|
|
|
call :kill_pid "%FOUND_PID%"
|
|
if "%KILL_OK%"=="1" (
|
|
echo [OK] Stopped %SERVICE_NAME% by port %TARGET_PORT% PID=%FOUND_PID%
|
|
set "STOP_RESULT=stopped"
|
|
)
|
|
set "SERVICE_NAME="
|
|
set "TARGET_PORT="
|
|
set "FOUND_PID="
|
|
exit /b 0
|
|
|
|
:stop_window_title
|
|
taskkill /FI "WINDOWTITLE eq %~1*" /T /F >nul 2>&1
|
|
exit /b 0
|
|
|
|
:kill_pid
|
|
set "KILL_OK=0"
|
|
set "KILL_TARGET_PID=%~1"
|
|
if "%KILL_TARGET_PID%"=="" exit /b 0
|
|
taskkill /PID %KILL_TARGET_PID% /T /F >nul 2>&1
|
|
if errorlevel 1 (
|
|
powershell -NoProfile -ExecutionPolicy Bypass -Command "try { Stop-Process -Id %KILL_TARGET_PID% -Force -ErrorAction Stop; exit 0 } catch { exit 1 }" >nul 2>&1
|
|
if not errorlevel 1 set "KILL_OK=1"
|
|
) else (
|
|
set "KILL_OK=1"
|
|
)
|
|
set "KILL_TARGET_PID="
|
|
exit /b 0
|