Monday, 8 February 2016

Ant and Windows Batch script for safe stopping, starting and checking status of a windows service


Sometimes windows services for stopping and starting takes time. Generally, we need to make sure that, service is stopped or started before we proceed to next steps. In such scenario, below script helps to achieve such goal. With help of Ant XML script and windows batch script, I have implemented this requirement.



ManageServices.cmd

echo off

       REM Query the Service Status, stop and start accordingly
       if "%2"=="START" (
          echo "Starting the Service - %1"
          sc START %1
  :WaitForServiceToStart1
                    echo "Service is still starting. Waiting for 5 seconds"
                    ping -n 5 127.0.0.1 > NUL
                    sc QUERY %1 | find /I "STATE" | find "RUNNING"
                      IF errorlevel  1 GOTO :WaitForServiceToStart1
                         echo "%1 has started"
             )

       if "%2"=="STOP" (
            echo "Stopping the Service - %1"
            sc STOP %1
        :WaitForServiceToStop1
               echo "Service is still stopping. Waiting for 5 seconds"
               ping -n 5 127.0.0.1 > NUL
               sc QUERY %1 | find /I "STATE" | find "STOPPED"
                  IF errorlevel  1 GOTO :WaitForServiceToStop1
                     echo "%1 has stopped"
           )


This script ManageServices.cmd helps to check whether services is stopped/started.

Below ant script has two targets, stop_service and start_service. The stop_service target helps to stop the service and start_service target helps to start the service by invoking ManageServices.cmd.

 <target name="stop_service" >
 <exec executable="ManageServices.cmd" failonerror="true">
   <arg value="${env.service.name}"/>
   <arg value="STOP"/>
   </exec>
 </target>
 
 <target name="start_service" >
 <exec executable="ManageServices.cmd" failonerror="true">
   <arg value="${env.service.name}"/>
   <arg value="START"/>
   </exec>
 </target>


I had a requirement in one project where I have to check a service whether it is running or not, if the service is running only, have to proceed further. To achieve this requirement have added below script to ManageServices.cmd file.


echo off
 if "%2"=="CHECK" (
        echo "Checking the Service - %1"
    :CheckingServiceState
           ping -n 5 127.0.0.1 > NUL
           sc QUERY %1 | find /I "STATE" | find "RUNNING"
    IF errorlevel  1 echo "%1 is NOT RUNNING">service_status.txt                                             
           )

Below Ant script has target name check_service which helps to check whether a service is running or not. If the service is not running the script fails and comes out of the ant script otherwise it continues.


<target name="check_service"> 
  <exec executable="ManageServices.cmd" failonerror="true">
   <arg value="${env.service.name}"/>   <arg value="CHECK"/>
   </exec>
   
  <fail message="${env.service.name} Service is NOT RUNNING">   <condition>
 <resourcecontains resource="service_status.txt" substring="NOT RUNNING"/>
  </condition>
  </fail>
</target>

0 comments:

Post a Comment