markwalker_
markwalker_

Reputation: 12859

How can an ANT build script kill a Windows process?

I'm working on extending an ANT build script to allow a TeamCity build agent to run Selenium tests.

In doing so there is a server required to start with selenium which isn't shutdown at the end. So I added an extra target to execute a taskkill on the exe name at the end of every TC build.

Does taskkill need the absolute path to the exe, because the following isn't working;

<target name="shutdown.server" depends="init.properties" description="Shutdown the server after Selenium">
    <exec osfamily="windows" executable="cmd.exe" spawn="true">
        <arg line="taskkill /f /t /im app.exe"/>
    </exec>
</target>

The process seems to have a few children which is why I've gone with /f /t but as I say, none of them shutdown at the moment.

Upvotes: 8

Views: 4913

Answers (2)

user6743474
user6743474

Reputation:

This is an old post and normally I use the timeout="milliseconds" , but for a long running process, I have found that the best way of killing an Ant launched process (especially java.exe threads within Eclipse) is to use pskill.exe.

Just load pskill64.exe into the project directory and run the following Ant build.

<project name="project" default="shutdown.java.tasks" basedir="../">
<target name="shutdown.java.tasks">

    <exec executable="path.to.pskill.in.your.project\pskill64">
        <arg value="java.exe" />
    </exec>

</target>

Typical output is:

shutdown.java:
 [exec] PsKill v1.16 - Terminates processes on local or remote systems
 [exec] Copyright (C) 1999-2016  Mark Russinovich
 [exec] Sysinternals - www.sysinternals.com
 [exec] 8 processes named java.exe killed.

BUILD SUCCESSFUL Total time: 387 milliseconds

Upvotes: 0

markwalker_
markwalker_

Reputation: 12859

Well that was easy;

<target name="shutdown.server" depends="init.properties" description="Shutdown the server after Selenium">
    <exec executable="taskkill">
        <arg line="/im app.exe /f /t"/>
    </exec>
</target>

Upvotes: 8

Related Questions