部署springBoot项目的脚本-windows
重启 restart.bat
说明:这里判断pid时,是通过端口查找到pid,然后在通过wmic 确认属于xxx.jar
@echo off
setlocal enabledelayedexpansionset JAR_NAME=xxx.jar
set port=8230
set LOG_DIR=logs\xxx
REM 创建日志目录
if not exist "%LOG_DIR%" mkdir "%LOG_DIR%"
rem 获取当前主机的所有端口号,循环判断当前端口是否已开启
for /f "usebackq tokens=1-5" %%a in (`netstat -ano ^| findstr %port%`) do (if [%%d] EQU [LISTENING] (set port-pid=%%e)
)
echo port-pid: %port-pid%if defined port-pid (for /f "skip=1 tokens=*" %%a in ('wmic process where "ProcessId='%port-pid%'" get CommandLine 2^>nul') do (set "cmdline=%%a"echo CommandLine=!cmdline!if not "!cmdline!"=="!cmdline:%JAR_NAME%=!" (echo compare successset "pid=%port-pid%"echo pid: !pid!goto break))
):breakecho restarting...
if defined pid (rem stopcall taskkill /f /pid %pid% >nul 2>&1if errorlevel 1 (echo Failed to stop the server.) else (echo Stop Success!)timeout /t 2 /nobreak >nul
)
echo starting collect program...
rem 配置jar包绝对路径
start "" /B javaw -jar "%JAR_NAME%" >> "%LOG_DIR%\app.log"
echo Starting Success!
pause
@echo off
setlocal enabledelayedexpansionrem 设置服务端口号
set JAR_NAME=xxx.jar
set port=8230
rem 获取当前主机的所有端口号,循环判断当前端口是否已开启
for /f "usebackq tokens=1-5" %%a in (`netstat -ano ^| findstr %port%`) do (if [%%d] EQU [LISTENING] (set port-pid=%%e)
)
echo port-pid: %port-pid%if defined port-pid (for /f "skip=1 tokens=*" %%a in ('wmic process where "ProcessId='%port-pid%'" get CommandLine 2^>nul') do (set "cmdline=%%a"echo CommandLine=!cmdline!if not "!cmdline!"=="!cmdline:%JAR_NAME%=!" (echo compare successset "pid=%port-pid%"echo pid: !pid!goto break))
):breakecho stopping...
if "%pid%"=="" (echo PID not found. Server may not be running.
)
if defined pid (call taskkill /f /pid %pid% >nul 2>&1if errorlevel 1 (echo Failed to stop the server.) else (echo Stop Success!)
)
pause
maven打包时,可以将脚本打包跟jar包在同一个目录
<fileSet><directory>bin/windows</directory><includes><include>restart.bat</include><include>stop.bat</include></includes><outputDirectory>/</outputDirectory></fileSet>