Batch Script - Looping for a Reboot

The batch script below, stops certain CA services, and then performs a check to see if any of these CA services are still running. If any are still running, it then performs another net stop of all the services. If none are running, then it performs a reboot of the server.

How do I put in a loop whereby the script checks if there are services running, tries to stop it and after 5 times of trying to stop the CA services, if it can't it jumps out and performs a reboot immediately.
_____________________________________________________________________________
:check-status

net stop "CA License Client"
sleep 15
net stop "CA-Unicenter"
sleep 15
net stop "CA-Unicenter (NR-Server)"
sleep 15
net stop "CA-Unicenter (Remote)"
sleep 15
net stop "CA-Unicenter (Transport)"
sleep 15
net stop "CA-Unicenter Discovery Scheduler"
sleep 15
net stop "CA-Unicenter NSM Auxiliary Services"
sleep 15
net stop "CA-Unicenter TNG Severity Propagation"
sleep 15
net stop "CA-Unicenter Worldview Agent"
sleep 15
net stop "CA-Workload Agents


for /f "tokens=*" %%a in ('net start ^| find /c /i "CA-"') do (
     if %%a EQU 0 (
         echo Rebooting Server at: %Currenttime%  %day%  %yyyy%%mm%%dd% >>c:\reboot\Logs\reboot-%hn%_%yyyy%%mm%%dd%.txt
         shutdown /r /f /t 10 >>c:\reboot\Logs\reboot-%hn%_%yyyy%%mm%%dd%.txt
    ) else (
         sleep 15
         set day=%date:~0,3%
         set Currenttime=%time:~0,8%
         echo Checking again if All CA Services have stopped at: %Currenttime%  %day%  %yyyy%%mm%%dd% >>c:\reboot\Logs\reboot-%hn%_%yyyy%%mm%%dd%.txt
         echo ---------------------------------------------------------------------------------------- >>c:\reboot\Logs\reboot-%hn%_%yyyy%%mm%%dd%.txt
         goto check-status
   )
)
ben1211Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wheelewCommented:
Have you considered using PowerShell?  You can register an event in WMI to query (say every 10 seconds or whatever your time interval).  You can use the cmdlet get-process <servicename> to get the status of services, pipe the results from the cmdlet to the 'kill' cmd to terminate running services (which may also save you reboot if the services do not stop gracefully).  You can also pass the results of the script to the event log rather than logging to a flat file.  It takes a little while to get comfortable with the commands, but the script above can be done with about six lines if you can get .net framework and powershell installed on your server.
0
ben1211Author Commented:
nope I've not considered using PowerShell. Am looking for some help to add in a loop please.
0
ben1211Author Commented:
I can get .net and powershell installed on my server. Would really appreciate some help with this script please.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Bart-VandyckCommented:
Hi Ben,

This is a small script in cmd

set a=1
set limit=11111

:loop
net stop Spooler > nul
ping -n 10 localhost > nul
sc query Spooler | findstr /i stopped
if %errorlevel%==0 goto exitloop


set a=%a%1
if not %a%==%limit% goto loop
echo Service did not stop
goto noreboot


:exitloop
echo Service stopped
Goto Reboot


:NoReboot
echo No reboot
goto end


:Reboot
Echo reboot
goto end


:end

It basically loops 5 times (the number of 1's you put in limit)
it stops the service
it waits a few seconds (ping -n 10 localhost > nul)
tests if the service is stopped ( sc query Spooler | findstr /i stopped)
If it stopped (errrorlevel==0) it exits and you can reboot or go to the next service)

PowerShell would give you much nicer and cleaner code thought ;-)

0
Bart-VandyckCommented:
The loop in PowerShell

$reboot=$false
for ($x=1; $x -lt 6; $x++)
{
      Stop-Service spooler -force
      if ($? -eq $true)
      {            
         $Reboot=$true
         break
      }

}

write-host "Reboot: $reboot"


0
ben1211Author Commented:
Hi Bart,

Thank you for the script. From the script that I have and posted above in my question, any idea how I can use that script and just look the entire thing 5 times and then exiting that loop and performing another command after those 5 loops have been satisfied?

from your script, you have a command to stop a specific service and then it loops back into that same procedure. How do I add in the additional services that I want to stop into the loop

:loop
net stop Spooler > nul
ping -n 10 localhost > nul
sc query Spooler | findstr /i stopped
if %errorlevel%==0 goto exitloop
0
Bart-VandyckCommented:
Hi Ben,

Do you mean you want to execute you're script 5 times? If so simple put it in a seperate cmd file call it from the loop routine with the command "call myscript.cmd" that way the entire thing will be executed 5 times. I saved your script with the name Checkstatus.cmd

I replaced the reboot command by creating a small file, to pass status back to the first cmd file. Then I call it, if the file found the script stops and reboots. Otherwise the scripts stops and nothing should happen.  

:check-status

net stop "CA License Client"
sleep 15
net stop "CA-Unicenter"
sleep 15
net stop "CA-Unicenter (NR-Server)"
sleep 15
net stop "CA-Unicenter (Remote)"
sleep 15
net stop "CA-Unicenter (Transport)"
sleep 15
net stop "CA-Unicenter Discovery Scheduler"
sleep 15
net stop "CA-Unicenter NSM Auxiliary Services"
sleep 15
net stop "CA-Unicenter TNG Severity Propagation"
sleep 15
net stop "CA-Unicenter Worldview Agent"
sleep 15
net stop "CA-Workload Agents


for /f "tokens=*" %%a in ('net start ^| find /c /i "CA-"') do (
     if %%a EQU 0 (
         echo Rebooting Server at: %Currenttime%  %day%  %yyyy%%mm%%dd% >>c:\reboot\Logs\reboot-%hn%_%yyyy%%mm%%dd%.txt
         echo Rebooting Server at: %Currenttime%  %day%  %yyyy%%mm%%dd% > rebootok.txt
    ) else (
         sleep 15
         set day=%date:~0,3%
         set Currenttime=%time:~0,8%
         echo Checking again if All CA Services have stopped at: %Currenttime%  %day%  %yyyy%%mm%%dd% >>c:\reboot\Logs\reboot-%hn%_%yyyy%%mm%%dd%.txt
         echo ---------------------------------------------------------------------------------------- >>c:\reboot\Logs\reboot-%hn%_%yyyy%%mm%%dd%.txt
         goto check-status
   )
)


Stop-Services.cmd

set a=1
set limit=11111
del rebootok.txt /q

:loop

Call checkstatus.cmd
if exist RebootOK.txt goto reboot
sleep 15
set a=%a%1
if not %a%==%limit% goto loop
echo Service did not stop
goto noreboot


:exitloop
echo Service stopped
Goto Reboot


:NoReboot
echo No reboot
goto end


:Reboot
echo Reboot
shutdown /r /f /t 10 >>c:\reboot\Logs\reboot-%hn%_%yyyy%%mm%%dd%.txt

goto end


:end


Probably you can do i in 1 script or with fewer lines, but when i write scripts I always try to keep in mind readability :-)



For the second question do you want to loop every 5 times before testing the next service. Or do you want test service 1  5 times, service 2 5 times,.......  and if a test fail stop runnig the batch file?

rgds,
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.