Apache – Batch script to tell if a website is up or down

apachebatch-filetomcatweb

My issue is that ping doesn't tell me if the website is up or down. I need to know if the website is up or down. If it is not up restart the script. If anyone can help me out that would be amazing.

The only thing I can think of is by grabbing the contents of the website and seeing if it is up or not.

I want to run this on the server with Apatche Tomcat.

@echo off


:first
PING -n 5 google.com | FIND "TTL" > nul
IF %errorlevel% == 0 (
echo Website is up. 
goto :first
) ELSE (
echo Website is down. Restarting service
goto :second
echo restart
echo ping
)


:: This calls the second loop
:second

:: This will stop the service
net stop TapiSrv 

ping -n 10 127.0.0.1 

:: This will start the service
net start TapiSrv

:: This check to see if the website is up
GOTO :first

Best Solution

I cobbled this together - it works here in Win 8.

Mind you it tells you if the website is responding with a message - it does not check if the page it serves is a normal operational page or a error message.

@echo off
if "%~1"=="" (
echo %0 www.url.com
echo Checks the status of the URL
pause
goto :EOF
)


 >"%temp%\geturl.vbs" echo Set objArgs = WScript.Arguments
>>"%temp%\geturl.vbs" echo url = objArgs(0)
>>"%temp%\geturl.vbs" echo pix = objArgs(1)
>>"%temp%\geturl.vbs" echo With CreateObject("MSXML2.XMLHTTP")
>>"%temp%\geturl.vbs" echo .open "GET", url, False
>>"%temp%\geturl.vbs" echo .send
>>"%temp%\geturl.vbs" echo a = .ResponseBody
>>"%temp%\geturl.vbs" echo End With
>>"%temp%\geturl.vbs" echo With CreateObject("ADODB.Stream")
>>"%temp%\geturl.vbs" echo .Type = 1 'adTypeBinary
>>"%temp%\geturl.vbs" echo .Mode = 3 'adModeReadWrite
>>"%temp%\geturl.vbs" echo .Open
>>"%temp%\geturl.vbs" echo .Write a
>>"%temp%\geturl.vbs" echo .SaveToFile pix, 2 'adSaveCreateOverwrite
>>"%temp%\geturl.vbs" echo .Close
>>"%temp%\geturl.vbs" echo End With


cscript /nologo "%temp%\geturl.vbs" http://%1 url.htm 2>nul 
if not exist url.htm (
echo site is down or access is denied
) else (
for %%a in (url.htm) do if %%~za GTR 0 echo site is up
del url.htm
)
del "%temp%\geturl.vbs"
pause