Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Batch Script to Check HTTP Status

12,772 views
Skip to first unread message

Henning Bredel

unread,
Feb 10, 2011, 3:17:41 AM2/10/11
to
Hey guys,

I need a bat script to check the availability of a tomcat service. I
thought about restarting tomcat if HTTP status code > 200 .. then I'd
just register that script as regular task, which in fact "monitors" my
service.

I googled to find out how to create web connections in a bat script, but
did not find much about it. Is this the common way doing this at all?
What would you prefer to keep a service alive?

Thanks for your comments

Henning

foxidrive

unread,
Feb 10, 2011, 3:32:59 AM2/10/11
to


Use ping to see if it is responding?

--
Regards,
Mic

Henning Bredel

unread,
Feb 10, 2011, 4:09:50 AM2/10/11
to

Yeah, thought about this, too. But ping tells only, if the server is up
and running. What I want is, to check if the context path to a website
is available. For example the URL

http://mydomain.com/context/path/

Some web logic knows how to react, when a user calls that URL and
responses with an HTTP status in the header. I'm interested in that
status to know, if the web logic has crashed. I'm not sure, if ping does
offer me such capabilities.

Henning

G. Morgan

unread,
Feb 10, 2011, 4:47:19 AM2/10/11
to
Henning Bredel <henning...@gmx.de> wrote:

>Some web logic knows how to react, when a user calls that URL and
>responses with an HTTP status in the header. I'm interested in that
>status to know, if the web logic has crashed. I'm not sure, if ping does
>offer me such capabilities.

Download
http://users.ugent.be/~bpuype/wget/#basic

Info
http://en.wikipedia.org/wiki/Wget

Tom Lavedas

unread,
Feb 10, 2011, 10:57:47 AM2/10/11
to

Here is a hybrid batch/JScript solution that doesn't require a third
party download ...

@set @x=0 /*
:: ChkHTTP.cmd
@echo off
setlocal
set "URL=http://www.google.com"
cscript /nologo /e:jscript "%~f0" %URL% | find "200" > nul
if %ErrorLevel% EQU 0 (
echo Web server ok % Put your code here %
) else (
echo Web server error reported
)
goto :EOF

JScript */
var x=new ActiveXObject("Microsoft.XMLHTTP");
x.open("GET",WSH.Arguments(0));x.send();
while (x.ReadyState!=4) {WSH.Sleep(50)};
WSH.Echo(x.status)

Thanks to '01MDM' for the concept.
_____________________
Tom Lavedas

Ted Davis

unread,
Feb 10, 2011, 10:58:14 AM2/10/11
to

You were given part of the usual solution - the tool to use but not how
to use it.

This pokes Yahoo and provided the one-line return:

wget --spider --no-verbose -t2 http://www.yahoo.com
2011-02-10 09:40:54 URL: http://www.yahoo.com/ 200 OK

The wget command line help switch is --help

Unfortunately, the report is sent to STDERR instead of STDOUT. I'm
pretty sure it returns an ERRORLEVEL.

--
Ted Davis (tda...@mst.edu)

Auric__

unread,
Feb 10, 2011, 11:58:58 AM2/10/11
to

That's what I thought of. I use wget to tell me if my home machine's IP
address has changed. I wrote a PHP script (on a free webhost) that takes as
its arguments an IP address, and then my home machine uses wget at every boot
to update the IP address:
wget -A zzz http://free.web.host/update.php?arg=ip.add.re.ss

('-A zzz' makes wget delete the downloaded file automagically.)

--
This manifesto has a single author.

Tom Lavedas

unread,
Feb 10, 2011, 3:26:45 PM2/10/11
to
On Feb 10, 10:57 am, Tom Lavedas <tglba...@verizon.net> wrote:
> On Feb 10, 3:17 am, Henning Bredel <henning.bre...@gmx.de> wrote:

A bit cleaner variation on the theme ...

@set @x=0 /*
:: ChkHTTP.cmd version 2


@echo off
setlocal
set "URL=http://www.google.com"
cscript /nologo /e:jscript "%~f0" %URL%

if %ErrorLevel% EQU 200 (
echo Web server ok
) else (
echo Web server error reported: %Errorlevel%
)
goto :EOF

JScript */
with(new ActiveXObject("Microsoft.XMLHTTP")){
open("GET",WSH.Arguments(0));send();
while(readyState!=4){WSH.Sleep(50)};
WSH.Quit(status)}
_____________________
Tom Lavedas

Brian Cryer

unread,
Feb 11, 2011, 6:27:42 AM2/11/11
to
"Henning Bredel" <henning...@gmx.de> wrote in message
news:4d53ab5d$0$6888$9b4e...@newsspool2.arcor-online.net...


Then try cryping - from http://www.cryer.co.uk/downloads/cryping/ it lets
you "ping" a url for example:
cryping -http www.cryer.co.uk/downloads/cryping
and its free.
--
Brian Cryer
http://www.cryer.co.uk/brian

Henning Bredel

unread,
Feb 11, 2011, 11:15:39 AM2/11/11
to
Am 10.02.2011 10:47, schrieb G. Morgan:

Oh, great .. didn't know that wget is also available under windows :)

Thanks all for your help

/h

sur...@electroons.com

unread,
Jan 9, 2014, 5:38:57 AM1/9/14
to
Hi Tom,

The JScript is working fine. But still i am facing problem with time required to run that JScript.

I have multiple weburl(Approx 40 url's)

I am calling the JScript as many times as i have URL's. But i am facing problem as some of the URL's are taking too much of time as they are not getting response from webserver. Is there any way to include timeout in JScript so that if Jscript is not responding for 60 seconds the program should break.

Regards,
Surojit

aakif...@gmail.com

unread,
Mar 4, 2014, 12:08:30 AM3/4/14
to
hey...it helped me...
we have Oracle Application server...I have introduced a loop in your script. I wanted to restart the OC4J instance until application is up.
Here is modified script (other code removed):

@set @x=0 /*
:: ChkHTTP.cmd version 2


@echo off
setlocal
set "URL=http://groups.google.com/forum"
:loop
cscript /nologo /e:jscript "%~f0" %URL%
if %ErrorLevel% EQU 200 (
echo Web server ok
) else (
echo Web server error reported: %Errorlevel%
goto :loop

Petr Laznovsky

unread,
Mar 4, 2014, 5:18:52 AM3/4/14
to
Dne 10.2.2011 10:09, Henning Bredel napsal(a):
Use WBOX

http://www.hping.org/wbox/wbox-3.zip

emoor...@gmail.com

unread,
Apr 12, 2016, 3:51:29 PM4/12/16
to
This is awesome. Just what I was looking for. Sometimes, when an internal application has failed the page might still return a status of 200 but still be down. So we adjusted it like this to test for a keyword on the returned page:

@set @x=0 /*
:: ChkHTTP.cmd
@echo off
setlocal
set "URL=http://www.yousite.com/"
cscript /nologo /e:jscript "%~f0" %URL% | find "Wishlist" > nul
if %ErrorLevel% EQU 0 (
echo Web server ok % Put your code here %
) else (
svcstart.bat ::(a batch file that restarts the windows service running the app)
)
goto :EOF
JScript */
var x=new ActiveXObject("Microsoft.XMLHTTP");
x.open("GET",WSH.Arguments(0));x.send();
while (x.ReadyState!=4) {WSH.Sleep(50)};
WSH.Echo(x.responseText)

marais...@gmail.com

unread,
May 30, 2017, 6:02:34 AM5/30/17
to
Hi This is awesome,

But how would you go around if your url is https?

Regards
Rikus

Mike Sanders

unread,
May 30, 2017, 7:26:29 AM5/30/17
to
marais...@gmail.com wrote:

> But how would you go around if your url is https?

If you don't mind using opensource software for Windows
then try wget, it will return HTTPS headers, download here:

https://eternallybored.org/misc/wget/

example 1:


@echo off

set URL=https://www.google.com/

wget %URL% --no-cache --server-response -O- 2>&1 | findstr HTTP

:eof


example 2:


@echo off

set URL=https://www.google.com/

wget %URL% --no-cache --server-response -O- 2>&1 | findstr OK > nul

if %errorlevel% neq 0 (echo server has problems & exit /b 1)

:eof


--
later on,
Mike

https://busybox.hypermart.net

marais...@gmail.com

unread,
May 30, 2017, 8:25:23 AM5/30/17
to
I tried this but its running for ever? Not sure what I am doing wrong

@set @x=0 /*
:: ChkHTTP.cmd
@echo off
setlocal
set "URL=https://localhost:8080/ping.xhtml"
cscript /nologo /e:jscript "%~f0" %URL% | find "200" > nul
echo going to check error
if %ErrorLevel% EQU 0 (
echo Web server ok
) else (
echo Weber server not ok
echo kill task and then restart service
)
JScript */
var x=new ActiveXObject("Msxml2.ServerXMLHTTP.6.0");
x.setOption(2,13056);
x.open("GET",WSH.Arguments(0));x.send();
while (x.ReadyState!=4) {WSH.Sleep(15000)};
WSH.Echo(x.status);

Mike Sanders

unread,
Jun 1, 2017, 8:34:52 AM6/1/17
to
marais...@gmail.com wrote:

> I tried this but its running for ever? Not sure what I am doing wrong

Excuse the delayed reply (work...) Well, its hard
to help without more detail buddy, lets make a checklist...

. both server/client are running?

. can you use something simpler like ping to at least test with?
(simpler often means a more robust solution, stay away from
complexity whenever possible)

. is the url address correct?

. can you access other urls in other domains?

. does any firewall stand between the server/client?

. have you tried pausing the script or placing some
echo &/or message boxes here & there in your script
to confirm variable values?

. have you tried other methods such as the one I suggested
to verify its not your hardware or script? (always remember
this rule - when something doesn't work as expected try
something different, else its wasting precious time to
continue to do the same thing over & over without a tweak)

. what else have you tried? (explain it to us by breaking
down the problem into small steps)
0 new messages