这是我在批处理文件中无限运行的 for 循环
FOR /L %%N IN () DO (
curl.exe http://localhost:8080/java_service/health_check
IF NOT ERRORLEVEL 0 (
echo "Service is down. Please check sgw_service.log for more info. " >> C:\data\logs\java_service.log
exit // THIS exit is NOT working, loop is still spinning
)
)
还有其他方法可以退出这个循环吗?
可以使用
GOTO
命令在满足退出循环条件时跳转到循环外的某个标签。这是您的代码的更新版本,它应该在服务关闭时退出循环:
@ECHO OFF
FOR /L %%N IN () DO (
curl.exe http://localhost:8080/java_service/health_check
IF NOT ERRORLEVEL 0 (
echo "Service is down. Please check sgw_service.log for more info. " >> C:\data\logs\java_service.log
GOTO :EXIT_LOOP
)
)
:EXIT_LOOP
ECHO Exiting loop
PAUSE
GOTO :EXIT_LOOP
命令会在服务宕机时跳转到标签EXIT_LOOP
。标签后的 ECHO Exiting loop
和 PAUSE
命令仅用于演示目的,可以替换为您自己的代码以退出脚本或执行任何必要的清理。