I'm executing a batch file with TaskScheduler "At system startup" on a Windows Server 2012 in Azure, and I observed that there's no network for 20-60 seconds after the start of the batch. Currently I added a constant wait with ping, and it works most of the time, but sometimes not. Can you suggest me a more robust method to wait until the network is available? (for sending email and/or http requests)
You seem to be intent on using batch for this so here goes. You may want to run this by the guys at SO since they are better scripters than I am. In fact, I got the main logic from (https://stackoverflow.com/questions/21245545/ping-test-using-bat-file-trouble-with-errorlevel) complete with the reason why testing if a ping will go through with this method is prefereable to others.
@echo off set IPaddress=%%1 REM add IP address as command line argument or just static :TEST ping -n 1 %IPaddress% | find "TTL=" >nul if errorlevel 1 ( goto RETRY ) else ( goto DOSTUFF ) :RETRY ping 127.0.0.1 -n 11>nul REM waits given amount of time, set to 10 seconds goto TEST :DOSTUFF do stuff REM Do stuff exit
Basically, it just pings at one second intervals, checks to see if a successful ping occurs. If so, it does whatever you're trying to script.
I did not hard code the IPaddress you want to check for basic ICMP connectivity for, but instead added it as a command line argument. Or you can just hard-code it.
You may want to double check the syntax as I did not test this at all.
EDIT: Changed ping value to 11 to reflect a real 10 second delay.
According to this answer on superuser, you can subscribe your scheduled task to the "network is available" event.
As long as you don't filter for a specific network, I think it should work.