@echo off :: This batch file uses the http-ping application to detect when :: a web server is not responding, or responds with a 5XX status :: code. It returns 0 if the server responds properly and 1 if not. :: It is intended for use with AlwaysUp :: (https://www.coretechnologies.com/products/AlwaysUp/) :: or Service Protector :: (https://www.coretechnologies.com/products/ServiceProtector/). :: Copyright © Core Technologies Consulting, LLC. All :: rights reserved. :: Here are a few variables that you should adjust. :: ** Please specify the URL you wish to ping, and the full path :: to the http-ping executable on your system ** SET url=http://localhost:80 SET http-ping-exe-path="C:\Temp\http-ping.exe" :: num-times-to-ping == The number of ping attempts that will be :: made. The default of 5 is probably OK, but you can increase :: it if errors are frequent (but tolerable). SET num-times-to-ping=5 :: num-seconds-between-pings == The time to wait between ping :: attempts. Increase this to give your server more time to get :: back on its feet after an error. SET num-seconds-between-pings=10 :: Loop "num-times-to-ping" times. FOR /L %%X IN (1,1,%num-times-to-ping%) DO CALL :loopbody %%X :: If we get here, then all ping attempts failed. echo All %num-times-to-ping% attempts of http-ping failed^! ^ Exiting with 1. exit 1 :loopbody :: Ping your web server once, returning the HTTP status code :: (or 0 if the ping failed to connect). %http-ping-exe-path% -e -n 1 %url% :: http-ping returns the HTTP status code or 0 if the ping :: failed to connect. The list of HTTP status codes is :: described here: :: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes :: For this script, we'll panic on 0 or 5XX (server error) :: and consider all other codes as a success. IF %ERRORLEVEL% GTR 0 ( IF %ERRORLEVEL% LSS 500 ( echo http-ping returned %ERRORLEVEL% - not an error. ^ Exiting with 0. exit 0 ) else IF %ERRORLEVEL% GTR 599 ( echo http-ping returned %ERRORLEVEL% - not an error. ^ Exiting with 0. exit 0 ) ) echo On attempt #%1, http-ping returned %ERRORLEVEL% - an error. :: If this was not the last attempt, pause before trying again. :: Since DOS doesn't have a "sleep" command, use ping with an :: invalid address and a timeout (in milliseconds) to wait for :: a while. You can adjust the timeout value :: (num-seconds-between-pings) above. IF %1 LSS %num-times-to-ping% ( SET /A milliseconds=%num-seconds-between-pings% * 1000 echo Pausing for %num-seconds-between-pings% second^(s^)^ before the next attempt... ping 1.1.1.1 -n 1 -w %milliseconds% > nul )