Link to home
Start Free TrialLog in
Avatar of ben1211
ben1211Flag for Malaysia

asked on

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
   )
)
Avatar of wheelew
wheelew

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.
Avatar of ben1211

ASKER

nope I've not considered using PowerShell. Am looking for some help to add in a loop please.
Avatar of ben1211

ASKER

I can get .net and powershell installed on my server. Would really appreciate some help with this script please.
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 ;-)

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"


Avatar of ben1211

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Bart-Vandyck
Bart-Vandyck

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial