简体   繁体   中英

Cannot run a Windows command via Ant, but can on cmd.exe

I have an ant macro I want to run;

<macrodef name="serviceTask">
    <attribute name="server"/>
    <attribute name="operation"/>
    <attribute name="service"/>
    <sequential>
        <echo message="sc \\@{server} @{operation} @{service}"/>
        <exec executable="sc.exe" failonerror="true">
            <arg line="\\@{server} @{operation} @{service}"/>
        </exec>
    </sequential>
</macrodef>

<target name="startTomcat">
    <echo message="Start Tomcat ${service} on ${server}"/>
    <serviceTask server="MyServer" operation="start" service="Tomcat8180"/>
</target>

But I get an RPC error:

startTomcat:
     [echo] Start Tomcat Tomcat8180 on pacdcdtadeva02
     [echo] sc \\pacdcdtadeva02 start Tomcat8180
     [exec] [SC] OpenSCManager FAILED 1722:
     [exec]
     [exec] The RPC server is unavailable.
     [exec]

stopTomcat:
     [echo] Stop Tomcat Service Tomcat8180 on pacdcdtadeva02
     [echo] sc \\pacdcdtadeva02 stop Tomcat8180
     [exec] [SC] OpenSCManager FAILED 1722:
     [exec]
     [exec] The RPC server is unavailable.
     [exec]
     [echo] -------------------------------------------------------------------
     [echo] --- Completed on 02/01/2011 05:11:42 PM
     [echo] -------------------------------------------------------------------

Now when I run this from the command line like

sc \\stage01 start Tomcat8180

the service starts/stops

C:\usr\svn_workspaces\xIVR\agent-ivr>sc \\stage01 start Tomcat8180

SERVICE_NAME: Tomcat8180
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 2  START_PENDING
                                (NOT_STOPPABLE,NOT_PAUSABLE,IGNORES_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x1
        WAIT_HINT          : 0xbb8
        PID                : 11228
        FLAGS              :

Perhaps this needs a shell environment. How about trying cmd /c sc ...

<exec executable="cmd.exe" failonerror="true">
    <arg line="/c sc \\@{server} @{operation} @{service}"/>
</exec>

This may or may not help you, but I had problems with the <exec> task with ant on a windows machine as well, in combination with the <arg line=..> statement. Ant just refused to treat the line of parameters as separate entities with spaces between them and I got weird errors like "unrecognised parameter" when it should have been a valid one.

My problems disappeared after I exchanged the one <arg line=> with one <arg value=...> for every parameter instead. I also had to do it like Raghuram specified, and wrap everything in a shell environment as well.

Might be worth a try.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM