如何退出批处理文件中永远运行的for循环?

问题描述 投票:0回答:1

这是我在批处理文件中无限运行的 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
    )
)

还有其他方法可以退出这个循环吗?

windows batch-file
1个回答
0
投票

可以使用

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
命令仅用于演示目的,可以替换为您自己的代码以退出脚本或执行任何必要的清理。

© www.soinside.com 2019 - 2024. All rights reserved.