three times loop

Microsoft Windows server 2008 standard -...
July 26, 2010 at 09:19:24
Specs: windows 2008 64 bit

I have created a batch file where I am checking if the service is started if it does, batch is checking another service, if it doesnt batch is starting it, the code looks like that:

REM Check to see if the applications are running
echo %date% %time% >> SERVICECHECK.LOG
net start | find "NOWArchive" >> SERVICECHECK.LOG 2>>&1
if not .%errorlevel%.==.0. goto TROUBLE1

echo REM NOWArchive Service is running >> SERVICECHECK.LOG 2>>&1
goto START2

REM Error, Start the NOWArchive application and check again. >> SERVICECHECK.LOG 2>>&1
net start "NOWArchive" >> SERVICECHECK.LOG 2>>&1
sleep 2 /quiet

REM Re-check and continue
echo %date% %time% >> SERVICECHECK.LOG
net start | find "NOWArchive" >> SERVICECHECK.LOG 2>>&1
if .%errorlevel%.==.0. goto START2
if not .%errorlevel%.==.0. goto TROUBLE1

echo REM NOWArchive Service is now running, but had to be restarted. >>
goto START2

The problem is I have endless loop here where if my service would not start it would loop and loop and loop... I would like to finish this loop lets say after three times. is it possible?

Also, if the service would not start I would like to get an email send to me.

How I can achieve this?

Thanks for your input

See More: three times loop

Report •

July 26, 2010 at 15:34:10
Uhm, I'm not quite sure if this is the best way to do it, but here's how I have accomplished this in the past.

Set Number (Or whatever you'd like) equal to 1 at the very start of the batch file. Have it run your code, add 1 to Number, then go back to the start.

Make sure you have

SET Number=1
Before your starting label.

Here's an example:


SET Number=1


ECHO Hello
IF "%Number%"=="3" GOTO End
SET /a Number +=1
GOTO Start


ECHO Complete

Hope I've helped.

Report •

July 27, 2010 at 06:37:11
thanks, this helped me a lot

I have another one question... is there any way to set the email sending when service would fail to start?

thanks a lot

Report •

July 28, 2010 at 08:34:00
Question: Why not just use Window's service recovery?

Report •

Related Solutions

Ask Question