@echo off setlocal enabledelayedexpansion chcp 65001 >nul title Quick Start set "SCRIPT_DIR=%~dp0" call :log_info SCRIPT_DIR=%SCRIPT_DIR% cd /d "%SCRIPT_DIR%" call :main pause && exit /b 0 :log_info call :GetFormattedTime if defined LOG_TITLE ( echo. echo [%FormattedTime%] - [INFO] - ==================== %LOG_TITLE% ==================== set "LOG_TITLE=" ) else ( echo [%FormattedTime%] - [INFO] - %* ) exit /b 0 :log_warn call :GetFormattedTime if defined LOG_TITLE ( echo. echo [%FormattedTime%] - [WARN] - ==================== %LOG_TITLE% ==================== set "LOG_TITLE=" ) else ( echo [%FormattedTime%] - [WARN] - %* ) exit /b 0 :log_error call :GetFormattedTime if defined LOG_TITLE ( echo. echo [%FormattedTime%] - [ERROR] - ==================== %LOG_TITLE% ==================== set "LOG_TITLE=" ) else ( echo [%FormattedTime%] - [ERROR] - %* ) exit /b 0 :DisplaySuccess timeout /t 3 /nobreak > nul echo. echo ======================================== echo %SERVER_HUMP% Services Successfully Started echo ======================================== for %%i in (%PORT_LIST%) do ( for /f "tokens=1,2 delims=:" %%a in ("%%i") do ( echo - %%b Server - Port %%a is listening ) ) echo ======================================== echo. exit /b 0 :Rar call :RandomStr 16 RAND_NAME set TARGET_DIR=%temp%\!CURRENT_DATE! if not exist "!TARGET_DIR!" mkdir "!TARGET_DIR!" call :log_info Creating encrypted archive... rar a -ep1 -hp"Password#!CURRENT_DATE!" "!TARGET_DIR!\!RAND_NAME!.rar" "!TEMP_SQL_DIR!\*" >nul if %errorLevel% neq 0 ( call :log_error Error: Failed to create encrypted archive pause && exit /b 1 ) if not exist "%SCRIPT_PATH%/patch/!CURRENT_DATE!" mkdir "%SCRIPT_PATH%/patch/!CURRENT_DATE!" call :log_info TEMP_SQL_DIR: %TEMP_SQL_DIR% mv !TEMP_SQL_DIR!/*.rar "%SCRIPT_PATH%/patch/!CURRENT_DATE!" rd /s /q "!TEMP_SQL_DIR!" 2>nul call :log_info Encryption completed successfully! call :log_info Archive: !TARGET_DIR!\!RAND_NAME!.rar call :log_info Password: Password#!CURRENT_DATE! exit /b 0 :GetSinglePidByPort setlocal call :RandomStr single_pid_tmp call :log_info netstat -ano ^| grep -E ":%1 " ^| grep "LISTENING" ^| awk "{print $NF}" ^| head -n 1 netstat -ano | grep -E ":%1 " | grep "LISTENING" | awk "{print $NF}" | head -n 1 > %single_pid_tmp%.txt set /p pid=<%single_pid_tmp%.txt call :log_info pid: %pid% del %single_pid_tmp%.txt endlocal & set pid=%pid% exit /b 0 :GetUsageNumberOfPidsByPort setlocal set "PORT=%~1" set /a "UNIQUE_COUNT=0" if "!PORT!"=="" ( call :log_error ERROR: Port number required endlocal & exit /b 0 ) set "TEMP_FILE=%TEMP%\port_%PORT%_%RANDOM%_%TIME::=_%.tmp" set "PID_LIST_FILE=%TEMP%\pids_%PORT%_%RANDOM%.tmp" call :log_info Checking for processes using port !PORT!... netstat -ano | grep -E ":!PORT! " > "!TEMP_FILE!" if !errorlevel! neq 0 ( call :log_info No process found using port !PORT! del "!TEMP_FILE!" 2>nul endlocal & exit /b 0 ) echo. > "!PID_LIST_FILE!" echo. set "LOG_TITLE=Processes using port !PORT!" call :log_info ignore for /f "tokens=5" %%a in (!TEMP_FILE!) do ( set "PID=%%a" :: grep -E "^^!PID! $" "!PID_LIST_FILE!" >nul findstr /x "!PID!" "!PID_LIST_FILE!" >nul if !errorlevel! neq 0 ( echo !PID! >> "!PID_LIST_FILE!" set /a "UNIQUE_COUNT+=1" call :log_info [!UNIQUE_COUNT!] PID: !PID! set "PROCESS_NAME=" set "SESSION_NAME=" set "SESSION_NUM=" set "MEM_USAGE=" for /f "tokens=1,2,3,4,5*" %%b in ('tasklist /fi "PID eq !PID!" /fo table ^| findstr /i "!PID!"') do ( if not "%%b"=="=" if not "%%b"=="Image" ( call :log_info "PROCESS_NAME=%%b" call :log_info "PID_VALUE=%%c" call :log_info "SESSION_NAME=%%d" call :log_info "SESSION_NUM=%%e" call :log_info "MEM_USAGE=%%f" ) ) if not "!PROCESS_NAME!"=="" ( call :log_info Process: !PROCESS_NAME! call :log_info Memory: !MEM_USAGE! ) echo. ) ) if !UNIQUE_COUNT! gtr 0 ( set "LOG_TITLE=PIDs using port !PORT!" call :log_info ignore :: type "!PID_LIST_FILE!" call :log_info Total: !UNIQUE_COUNT! process ) del "!TEMP_FILE!" 2>nul del "!PID_LIST_FILE!" 2>nul endlocal & exit /b %UNIQUE_COUNT% :ListenPortList set all_ports_up=1 for %%i in (%PORT_LIST%) do ( for /f "tokens=1,2 delims=:" %%a in ("%%i") do ( set port=%%a set service=%%b netstat -an | find ":%%a" > nul if !errorlevel! neq 0 ( set all_ports_up=0 :: call :log_info "!service! Server (Port: %%a) isn't ready..." ) ) ) if !all_ports_up! equ 1 ( call :DisplaySuccess exit /b 0 ) set /a counter+=CHECK_INTERVAL if !counter! geq %MAX_WAIT% ( call :log_error "Services did not start within !counter! seconds" exit /b 1 ) :: call :log_info "Waiting for services... (!counter!/%MAX_WAIT% seconds)" echo. timeout /t %CHECK_INTERVAL% /nobreak > nul goto ListenPortList :RandomStr setlocal set "len=%~1" set "varname=%~2" if "!varname!"=="" set "varname=RAND_STR" set "chars=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" set "random_str=" for /l %%i in (1,1,!len!) do ( set /a rand_index=!random! %% 62 call set "char=%%chars:~!rand_index!,1%%" set "random_str=!random_str!!char!" ) endlocal & set "%varname%=%random_str%" exit /b 0 :Start setlocal set DESCRIPTION=%1 set SCRIPT_PATH=%2 set SCRIPT_FILE=%3 set PARAMS= shift /3 :params_loop if not "%~1"=="" ( set "PARAMS=%PARAMS% %~3" shift goto params_loop ) if exist "%SCRIPT_PATH%\%SCRIPT_FILE%" ( echo. call :log_info Starting %DESCRIPTION%... cd /d "%SCRIPT_PATH%" start "%DESCRIPTION% Startup" /B cmd /c "%SCRIPT_FILE% %PARAMS%" if errorlevel 1 ( call :log_error %DESCRIPTION% start Failed. pause && exit /b 1 ) ) else ( call :log_error Script file not found: %SCRIPT_PATH%\%SCRIPT_FILE% pause && exit /b 1 ) endlocal exit /b 0 :GetFormattedTime for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "datetime=%%a" set "FormattedTime=%datetime:~0,4%-%datetime:~4,2%-%datetime:~6,2% %datetime:~8,2%:%datetime:~10,2%:%datetime:~12,2%" exit /b 0 :GetDate for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "datetime=%%a" set "FormattedDate=%datetime:~0,4%%datetime:~4,2%%datetime:~6,2%" exit /b 0 :main setlocal :: nginx call :GetUsageNumberOfPidsByPort 18080 if !ERRORLEVEL! neq 0 ( call :log_info Found !ERRORLEVEL! processes. call :GetSinglePidByPort 18080 set "LOG_TITLE=Killing Process !pid!" call :log_info ignore :: taskkill -f -pid !pid! >nul taskkill -f -im home-web.exe ) timeout /t 1 /nobreak >nul call :Start "Nginx" "%SCRIPT_DIR%nginx" "home-web.exe" "-c" "conf/web.conf" :: redis timeout /t 3 /nobreak >nul call :Start "Redis" "%SCRIPT_DIR%redis" "redis-server.exe" "redis.conf" :: minio timeout /t 3 /nobreak >nul call :Start "MinIO" "%SCRIPT_DIR%io" "minio-server.bat" :: rocketmq timeout /t 3 /nobreak >nul call :Start "RocketMQ" "%SCRIPT_DIR%rocketmq\sbin" "rocketmq.bat" set PORT_LIST=9876:RocketMQ-NameServer 10911:RocketMQ-Broker 8081:RocketMQ-Proxy 8088:RocketMQ-Dashboard set SERVER_HUMP=RocketMQ set CHECK_INTERVAL=5 set MAX_WAIT=60 set counter=0 call :ListenPortList endlocal cmd /k