0

“timeout /t 10 /nobreak > NUL”卡住了,没有出来执行下一条语句。我编写了一个下面的脚本,它在我的开发机器上按预期执行,并且也在少数测试机器上进行了测试。它等待 10 秒,然后再次开始执行(也在安全模式下测试),但是在其中一台机器 windows 2003 服务器上,许可证已过期,我的脚本在安全模式下运行,但是在执行上述超时语句时,它不会执行下一个声明。

有人可以告诉我原因(如果有的话)以及解决方案是什么?


set restart=1
...
... some other code
...
setlocal ENABLEDELAYEDEXPANSION
:WaitToRestart
if "%restart%"=="1" (
    echo waiting for restart >> log.txt
    shutdown /r /t 0
    if "!errorlevel!" NEQ "0" (
        echo error: !errorlevel! while restarting so wait for 10 sec >> log.txt
        timeout /t 10 /nobreak > NUL
        goto WaitToRestart
    )
    echo error: !errorlevel! you shuld not be here to see after death >> log.txt
)
setlocal DisableDelayedExpansion

在我的开发机器上,该脚本运行良好(Windows 10),但在 Windows 2003 上的生产环境中(它作为启动脚本运行,在安全模式下启动后运行)。

所以我用命令“PING localhost -n 6 >NUL”更改了“超时”命令,但它在旋转 10、12 次后也卡住了。当我删除“PING”命令时,它会旋转很长时间,直到命令“shutdown”成功执行。

它看起来像是特定于机器的问题,并且可能由于许可证过期问题而停止,因为我从物理机的备份创建了 VM

4

1 回答 1

1

If you're just trying to get a wait function, you can manipulate ping to do that.

ping 0 -w 1000 -n 10 >nul

Breaking this apart piece by piece, cmd expands 0 to 0.0.0.0, which is the IP address cmd will try to ping (it doesn't exist). -w specifies the number of milliseconds ping will wait before timing out a request. -n specifies how many requests you'd like to send (so your total duration, in seconds is w/1000 * n). >nul is not ping-specific; it nullifies the output to the console.

By pinging an IP that doesn't exist, you force ping to timeout with every request, which gets you a dependable wait time.

于 2019-12-04T08:59:27.130 回答