::1.jar包、启动日志、启动脚本、占用端口、启动成功标志 set prefix=%1 set port=%2 set profile=%3 set Xmx=%4 set successFlag=%5 set jarFile=g:\rsdun-cloudfish\%prefix%.jar set startBat=g:\rsdun-cloudfish\jenkins\startJar.bat set startLog=%prefix%-%port%.log set currentDate=%date:~0,4%-%date:~5,2%-%date:~8,2% set logPath=g:\rsdun-cloudfish\startLogs\%currentDate% set logFile=%logPath%\%startLog%
::2.杀死正在运行的项目进程 echo =========================================== kill端口:%port% =========================================== setlocal enabledelayedexpansion for /f "tokens=1-5" %%a in ('netstat -ano ^| findstr "%port%"') do ( if "%%e%" == "" ( set pid=%%d ) else ( set pid=%%e ) ) if NOT "!pid!" == "" ( taskkill /f /pid !pid! )
::3.删除旧的日志文件 echo =========================================== 删除日志文件:%logFile% =========================================== :delFile if exist %logFile% ( del %logFile%>nul 2>nul goto delFile )
::cronolog不会自动创建文件夹,需要手动创建 if not exist %logPath% ( md %logPath% )
::5.打印启动日志 echo =========================================== 开始打印启动日志 =========================================== set line=0 :while ::检查日志文件是否存在 if not exist %logFile% ( goto while ) set content= @for /f "tokens=1* delims=:" %%i in ('findstr/n .* %logFile%') do ( @if %%i GTR %line% ( set line=%%i set content=!content!%%j set content=!content:~-2000! echo %%j ) ) ::检查项目是否输出启动成功标志 set exists_flag=false echo !content!|find %successFlag%>nul&&set exists_flag=true if "%exists_flag%"=="true" ( echo =========================================== 项目启动成功! =========================================== exit ) goto while
exit
startJar.bat
1 2 3 4 5 6 7 8 9 10 11
@echo off set Xmx=%1 set jarFile=%2 set options=%3 set startLog=%4 ::由于bat中无法将=作为参数传入,因此传入@,这里替换为= set "options=%options:@==%" set BUILD_ID=dontKillMe ::使用cronolog来对日志进行分割 java %Xmx% -jar %jarFile% %options% | G:\rsdun-cloudfish\jenkins\cronolog.exe G:\rsdun-cloudfish\startLogs\%%Y-%%m-%%d\%startLog% exit
::1.jar包、启动日志、启动脚本、占用端口、启动成功标志 set prefix=%1 set port=%2 set profile=%3 set Xmx=%4 set successFlag=%5 set jarFile=g:\rsdun-cloudfish\%prefix%.jar set startBat=g:\rsdun-cloudfish\jenkins\startJar.bat set startLog=g:\rsdun-cloudfish\startLogs\%prefix%-%port%.log
::2.杀死正在运行的项目进程 echo =========================================== kill端口:%port% =========================================== setlocal enabledelayedexpansion for /f "tokens=1-5" %%a in ('netstat -ano ^| findstr "%port%"') do ( if "%%e%" == "" ( set pid=%%d ) else ( set pid=%%e ) ) if NOT "!pid!" == "" ( taskkill /f /pid !pid! )
::3.删除旧的日志文件 echo =========================================== 删除日志文件:%startLog% =========================================== :delFile if exist %startLog% ( del %startLog%>nul 2>nul goto delFile )
::5.打印启动日志 echo =========================================== 开始打印启动日志 =========================================== set line=0 :while ::检查日志文件是否存在 if not exist %startLog% ( goto while ) set content= @for /f "tokens=1* delims=:" %%i in ('findstr/n .* %startLog%') do ( @if %%i GTR %line% ( set line=%%i set content=!content!%%j set content=!content:~-2000! echo %%j ) ) ::检查项目是否输出启动成功标志 set exists_flag=false echo !content!|find %successFlag%>nul&&set exists_flag=true if "%exists_flag%"=="true" ( echo =========================================== 项目启动成功! =========================================== goto end ) goto while
:end ::关闭cronolog.exe进程,停止日志打印 echo =========================================== 终止日志打印进程! =========================================== wmic process where "Name='cronolog.exe' and CommandLine like '%%%prefix%%%'" delete ::这一步是用来关闭 `cmd.exe` 进程的, `cmd.exe`需要主动`kill`,否则会一直运行 wmic process where "Name='cmd.exe' and CommandLine like '%%%prefix%%%'" delete
exit
startJar.bat
1 2 3 4 5 6 7 8 9 10 11
@echo off set Xmx=%1 set jarFile=%2 set options=%3 set startLog=%4 ::由于bat中无法将=作为参数传入,因此传入@,这里替换为= set "options=%options:@==%" set BUILD_ID=dontKillMe ::使用cronolog来对日志进行分割 java %Xmx% -jar %jarFile% %options% | G:\rsdun-cloudfish\jenkins\cronolog.exe %startLog% exit