批处理文件超时命令跳到10k,30k,40k秒 [英] batch file timeout command jumping to 10k, 30k, 40k seconds
问题描述
我当前正在运行一组利用批处理文件中的超时功能的命令.超时功能使用一个变量将其暂停多少秒,我认为这可能会引起问题,但是我非常需要它能够正常运行.
I'm currently running a set of commands that utilizes the timeout function in my batch files. The timeout function uses a variable for how many seconds it should pause, and I think that may be causing issues, but I desperately need it to function properly.
我已经更新了我的问题,其中详细说明了可能发生的情况.
I've got an update to my question which details more about what may be happening.
我使用了评论中的一些建议来尝试修复脚本,但这没有用.
I used some suggestions that I had in the comments to try and fix my script, but it did not work.
这是我的剧本:
:connectToTheInternet
setlocal EnableDelayedExpansion
set timeout=2
echo checking internet connection
echo.
:connectToTheInternetRestart
ping -n 1 google.com | find /i "TTL=" >NUL && (
echo Internet connection already established, checking script versions.
echo.
goto scriptVersion
REM toupd
) || (
if "%COUNTER%"=="0" (
echo Creating wifi profile.
echo.
set /A COUNTER=1
goto createWifiProfile
)
SET /A "tries = (%COUNTER%-1)+1"
REM if failed endonfail times, give up
if "%COUNTER%"=="%endonfail%" (
echo Error: Could not connect to network %tries% times, stopping script.
echo.
goto endNoShutdown
)
echo.
echo Attempt #%COUNTER%.
echo.
REM raise waiting time between connections
SET /A "modulo = (%COUNTER%-1) %% 2"
if "%modulo%" EQU "0" (
set /A timeout=timeout+2
echo failed %tries% times in a row. Increasing wait time between actions to %timeout% seconds.
set /A COUNTER=COUNTER+1
) else (
set /A COUNTER=COUNTER+1
)
REM disconnect existing network
netsh wlan disconnect
netsh wlan delete profile name="%wifissid%"
timeout /t 1 /nobreak
echo.
REM attempt connection
netsh wlan add profile filename="connect.xml"
netsh wlan connect name="%wifissid%" ssid="%wifissid%" >NUL
echo.
echo Wait time is currently !timeout! seconds.
timeout /t !timeout! /nobreak
echo.
REM check pings
ping -n 1 google.com | find /i "TTL=" >NUL && (
echo Successfully connected. Checking script version.
echo.
goto scriptVersion
) || (
set /a COUNTER2=COUNTER+1
echo Connection attempt failed. Starting attempt #%COUNTER2% in 3 seconds.
echo.
timeout /t 3 /nobreak
cls
goto connectToTheInternetRestart
)
)
我的问题特别是timeout /t !timeout! /nobreak
,它应该等待可变的时间.问题是,有时(仅在设备成功连接到Internet时发生,但并非每次设备连接到互联网时),超时跳到10,000秒,30,000秒或其他一些随机的高数字(而且它们确实是随机的,我看过的时间大约是12,536秒.)根据超时变量,该数值应始终小于12.是的,我的意思是 秒 ,而不是 毫秒 .您可以想象,10k秒(大约21天)与10秒之间有很大的差异.
My problem is specifically the timeout /t !timeout! /nobreak
which should wait for a variabled amount of time. The problem is, sometimes (it only happens when the device successfully connects to the internet, but not every time the device connects to the internet) the timeout jumps up to 10,000 seconds, 30,000 seconds or some other random high number (And they really are random, I've seen some at like 12,536 seconds.) When it should always be less than 12 based on the timeout variable. Yes, I mean seconds NOT milliseconds. As you can imagine, there is a big difference between 10k seconds (roughly 21 days) vs 10 seconds.
我不知道是什么原因引起的,或者如何解决它,并希望获得帮助.
I have no idea what could be causing this, or how to solve it and would love some help.
推荐答案
这是您的评论引起的左场思考
Here's a left-field thought, brought about by your comment
(仅当设备成功连接到互联网时才会发生,但并非每次设备连接到互联网时都会发生
(it only happens when the device successfully connects to the internet, but not every time the device connects to the internet)
在连接到互联网之前,日期,时间和时区是否正确?连接将同步时间-这可能会使您超时!
Before it connects to the internet, is the date, time and timezone correct? Connecting will synchronise the time - which may muck up your timeouts!
就像我说的:左场,但是...
Like I said: left-field, but...
这篇关于批处理文件超时命令跳到10k,30k,40k秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!