MUKHTAR INAMDAR
MUKHTAR INAMDAR

Reputation: 115

timeout /t 10 /nobreak > NUL is getting stuck and not coming out to execute next statement on windows 2003 server VM

"timeout /t 10 /nobreak > NUL" is getting stuck and not coming out to execute the next statement. I have written a below script which executes as expected on my dev machine and also tested on few test machines also. it waits for 10 seconds and the again start execution (tested in safe mode also), but on one of the machine windows 2003 server which license is expired my script runs in safe mode, but when executing the above timeout statement it doesn't execute the next statement.

Can someone please tell me the reason if any and what will be the solution?


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

On my dev machine, this script runs well (windows 10) but stuck on the production environment (where it runs as a startup script which run after boot in safe mode) on Windows 2003.

So I change the "timeout" command with command "PING localhost -n 6 >NUL" but it also stuck after rotating 10, 12 times. And when I removed "PING" command also then it rotates for a long time till the command "shutdown" executed successfully.

It looks like the machine-specific issue and maybe stopped because of license expire issue as the I have created VM from backup of physical machine

Upvotes: 1

Views: 5226

Answers (1)

Drake Johnson
Drake Johnson

Reputation: 664

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.

Upvotes: 1

Related Questions