问题与背景
在实际的部署过程中,尤其是需要跟anaconda整合,遇到了bat脚本需要启动bat脚本的套娃操作,过程中遇到了单独启动bat脚本没问题,用bat启动bat就出问题的情况。最终发现是跳转的时候忘记添加 /d 参数了,导致了只能在一个盘下操作,条状就不行的情况,特此记录一下,最终的靠谱的脚本。
最佳实践
主监控脚本
主监控脚本,需要开机启动,每n秒自动执行一次,监控端口是不是被监听,进程是不是运行,如果没有就重新启动一下。
@echo off
chcp 65001
title "算法中间件生命周期监控"
rem 定义循环间隔时间: 10s
set /a secs=10
set log=D:\middleware_log.txt
set main_port=22972
set main_location=D:/codeRepo/Middleware_Main/start.bat
set main_name=middleware_main
set main_dir=D:/codeRepo/Middleware_Main
set ocr_port=22976
set ocr_location=D:/codeRepo/Middleware_PaddleOCR/start.bat
set ocr_name=middleware_ocr
set ocr_dir=D:/codeRepo/Middleware_PaddleOCR
echo 算法中间件监控脚本启动!
echo ================================================================================================== >> %log%
call:monitor_middleware
:monitor_middleware
::检查Main
call:check_middleware %main_port% %main_location% %main_name% %log% %main_dir%
::检查ocr
call:check_middleware %ocr_port% %ocr_location% %ocr_name% %log% %ocr_dir%
echo ================================================================================================== >> %log%
choice /t %secs% /d y /n >nul
goto monitor_middleware
:check_middleware
echo [%date:~0,4%-%date:~5,2%-%date:~8,2% %time:~0,2%:%time:~3,2%:%time:~6,2%] 开始检查 %3 运行情况 ......>> %4
set state=0
for /f "tokens=3 delims=: " %%a in ('netstat -an') do if "%%a"=="%1" set state=1
if %state%==1 (
echo [%date:~0,4%-%date:~5,2%-%date:~8,2% %time:~0,2%:%time:~3,2%:%time:~6,2%] %3 正常运行!>> %4
)else if not %state%==1 (
echo [%date:~0,4%-%date:~5,2%-%date:~8,2% %time:~0,2%:%time:~3,2%:%time:~6,2%] %3 未启动! >> %4
echo [%date:~0,4%-%date:~5,2%-%date:~8,2% %time:~0,2%:%time:~3,2%:%time:~6,2%] %3 正在启动中 ...... >> %4
cd %5
start %2
choice /t 5 /d y /n >nul
for /f "tokens=3 delims=: " %%b in ('netstat -an') do (
if "%%b"=="%1" (
echo [%date:~0,4%-%date:~5,2%-%date:~8,2% %time:~0,2%:%time:~3,2%:%time:~6,2%] %1 启动成功!>> %4
)
)
)
set state=
echo ------------------------------------------------------------------------------------------------->> %4
goto:eof
:end
子组件启动脚本
需要注意切换路径的时候的参数。
@echo off
chcp 65001
title "算法中间件启动终端MAIN"
cd /d D:/codeRepo/Middleware_PaddleOCR
call C:/soft/anaconda/Scripts/activate.bat C:/soft/anaconda/envs/middleware_paddleocr
call python ocr.py