【问题标题】:How do exit out from forever running for loop in batch file?如何退出批处理文件中永远运行的循环?
【发布时间】:2023-02-24 02:44:06
【问题描述】:

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

    当满足退出循环条件时,可以使用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:datalogsjava_service.log
            GOTO :EXIT_LOOP
        )
    )
    
    :EXIT_LOOP
    ECHO Exiting loop
    PAUSE
    

    GOTO :EXIT_LOOP命令会在服务宕机时跳转到标签EXIT_LOOP。标签后的 ECHO Exiting loopPAUSE 命令仅用于演示目的,可以替换为您自己的代码以退出脚本或执行任何必要的清理。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-07
      • 2021-10-26
      • 2014-02-21
      • 1970-01-01
      • 2021-12-14
      • 2012-09-14
      • 1970-01-01
      • 2015-03-28
      相关资源
      最近更新 更多