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?
 
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
 
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
ben1211Author Commented:
I can get .net and powershell installed on my server. Would really appreciate some help with this script please.
0
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.