1

My requirement is to start window service from Ant. here is my code:

<target name="copy">
    <exec executable="net">
        <arg value="START"/>
        <arg value="SomeService"/>          
    </exec>
</target>

it works perfectly fine if service is yet not started. But if service is already started it return me the exception [exec] Result: 2

Ashish Aggarwal
  • 3,018
  • 2
  • 23
  • 46
  • 2
    I believe, you should first find whether service is running, start otherwise. Find [here](http://stackoverflow.com/questions/3325081/how-to-check-if-a-service-is-running-via-batch-file-and-start-it-if-it-is-not-r) on how check the same – Rao Feb 04 '16 at 09:13
  • 1
    ANT is a java build tool. Wouldn't it be more appropriate to use a Configuration management tool like chef to manage services on your server? – Mark O'Connor Feb 06 '16 at 07:50
  • I disagree. ANT is not solely intended to build java applications. It is rather generally suited to "pilot any type of process which can be described in terms of targets and tasks". So checking whether a serive is running or not is perfectly within the scope of an ANT target. – buddemat Oct 19 '20 at 08:08

1 Answers1

0

(1) Using a BAT file: As pointed out in Rao's comment, you can write a batch file that checks whether the service is running and starts it if not, as described here: How to check if a service is running via batch file and start it, if it is not running?. This could be called using an <exec> command in an ant target.


(2) Using just an ANT target: Alternatively, if you do not want an extra batch file you can call the appropriate one-line command through an ant target with an <exec> statement via cmd /C. In my example, the service name is "JBossEAP7", this would be the string that needs to be substituted for your service name.

<target name="run-jboss-service-if-not-already-running">
    <exec dir="bin/" executable="cmd" spawn="false" failonerror="true">
        <arg value="/c"/>
        <arg value="&quot;for /f &quot;tokens=3 delims=: &quot; %H in ('&quot;sc query JBossEAP7 | findstr &quot;STATE&quot;&quot;') do if not &quot;%H&quot;==&quot;RUNNING&quot; net START JBossEAP7 &quot;" />
    </exec>
</target>

This target executes cmd which calls sc query to check the status of the service (as described here https://stackoverflow.com/a/3325123/14015737) and calls net START if the status is anything other than RUNNING.


(3) Using a series of ANT targets: If you do not necessarily want to start the service, but want to do other things depending on whether it is running or not, or if other targets need to be conditionally executed, you can use a series of dependent ant targets to achieve this (in my example, I am still just starting the service, but you could do other stuff).

<target name="check-jboss-service-running">
    <exec executable="cmd" spawn="false" failonerror="false" resultproperty="service.result">
        <arg value="/c"/>
        <arg value="&quot;for /f &quot;tokens=3 delims=: &quot; %H in ('&quot;sc query JBossEAP7 | findstr &quot;STATE&quot;&quot;') do if not &quot;%H&quot;==&quot;RUNNING&quot; exit /B 1&quot;" />
    </exec>
    <condition property="jboss-service.running">
        <equals arg1="0" arg2="${service.result}"/>
    </condition>
</target>

<target name="start-jboss-service" depends="check-jboss-service-running" unless="jboss-service.running">
    <exec executable="net" spawn="false" failonerror="true" resultproperty="net.result">
        <arg value="START"/>
        <arg value="JBossEAP7"/>
    </exec>
    <condition property="jboss-service.running">
        <equals arg1="0" arg2="${net.result}"/>
    </condition>
</target>

<target name="whatever-comes-next" depends="start-jboss-service" if="jboss-service.running">
    <echo>
        JBoss service is running
    </echo>
</target>

A bit more detail:

  • The first target (check-jboss-service-running) again runs an <exec> statement that executes cmd which calls sc query to check the status of the service. If not RUNNING, it exits the cmd returning 1. Next, depending on the return code, the property jboss-service.running is either set or not in the <condition> statement.

  • The second target (start-jboss-service) is only executed if the property jboss-service.running has not been set. It starts the service and, if successful, sets the property jboss-service.running in the <condition> statement.

  • The third target can be whatever you want to do next, once the service is running. It is only executed if the property jboss-service.running has been set, which should have either been done by the first or the second target.


Two remarks:

(1) There are of course states other than "RUNNING" and "STOPPED". So your service e.g. is currently starting up or shutting down, the net command will be executed, even though you may not want that.

(2) Depending on the service you are checking or starting, you may need to introduce delays to make sure that whatever the service does is actually up and running. E.g. in my example, the JBoss may be running, but it takes additional time before the deployed web applications are responsive.

buddemat
  • 4,552
  • 14
  • 29
  • 49