4

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)

2
  • Possible solution here: superuser.com/questions/48231/…
    – Byron C.
    Jul 14, 2014 at 23:20
  • 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
    Jul 15, 2014 at 8:38

3 Answers 3

2

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.

1
  • 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!
    – Wolfgang
    May 9, 2016 at 18:39
1

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

2
  • 2
    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.
    – robert4
    Jul 15, 2014 at 13:29
  • Confirmed: this does not wait for the network to become available, instead omits the task if there's no network at startup.
    – robert4
    Sep 15, 2014 at 20:54
0

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.

2
  • 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
    Jul 15, 2014 at 13:21
  • 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.
    – Nitz
    Jul 15, 2014 at 14:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .