How to wait for network in a batch script when booting on Windows Server?

20,094

Solution 1

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.

Solution 2

Use the "Start only if this network connection is available" option. enter image description here

Share:
20,094

Related videos on Youtube

robert4
Author by

robert4

Updated on September 18, 2022

Comments

  • robert4
    robert4 over 1 year

    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)

    • Byron C.
      Byron C. almost 10 years
      Possible solution here: superuser.com/questions/48231/…
    • robert4
      robert4 almost 10 years
      No, those are not relevant to this question. Those solutions wait for a fixed period, this is what I do already. I would like to wait until the network becomes ready for remote http/smtp requests, preferably without hard-wiring the name of a network connection in Windows.
  • robert4
    robert4 almost 10 years
    Good idea, but I need to run this batch at system start only. In the startup script I would have to check for the presence of a recent "network is available" event... It is feasible with wevtutil.exe, but not simpler than monitoring the network with cURL or the like.
  • robert4
    robert4 almost 10 years
    I thought that the meaning of this check box is to omit the task if the network is not available at the time of the trigger (i.e. at startup) instead of waiting until the network becomes available. I'll give it a try.
  • Nitz
    Nitz almost 10 years
    I used a similar solution once (task scheduler waiting for network event), under the assumption that my servers aren't getting a lot of network-is-up events after being on.
  • robert4
    robert4 over 9 years
    Confirmed: this does not wait for the network to become available, instead omits the task if there's no network at startup.
  • Wolfgang
    Wolfgang almost 8 years
    The only syntax error is that REM is actually a command, so you can't put it on the same line as another command. Move the comments to the line above and this script works perfectly. Thank you!