2017-01-07 1 views
2

<exec>을 사용하여 긴 빌드 작업을 실행하는 개미 작업이 있습니다. Ant는 Windows 명령 행의 배치 파일에 의해 시작됩니다. 창을 닫아 개미 작업을 종료하면 <exec>에 의해 시작된 프로세스가 계속 실행됩니다. 앤트 프로세스 자체가 종료되었을 때 어떻게 생성 된 프로세스를 종료 할 수 있습니까? 명령 행 창을 닫을 때아파치 개미 : 개미 프로세스가 종료 될 때 <exec>에 의해 시작된 프로세스 종료.

<exec executable="${make.executable}" dir="${compile.dir}" failonerror="true"> 
    <arg line="${make.parameters}" /> 
</exec> 

java 프로세스를 실행 개미가 제대로 종료 :

개미 1.10.0은 오라클 JDK 8. 프로세스를 시작하기 작업과 윈도우 7의 x64에 사용하는 것은 유사하다.

답변

1

여기에 가능한 솔루션입니다 :

  • 배치 스크립트는 antPidFile라는 이름의 인수와 함께 개미를 시작합니다.
  • Ant 스크립트는 Java jps 도구를 사용하여 java.exe Ant 스크립트를 실행하는 프로세스의 PID를 가져옵니다.
  • Ant 스크립트는 PID를 antPidFile에 기록합니다.
  • Ant 스크립트가 하위 프로세스를 생성합니다.
  • Ant 종료 및 제어가 배치 스크립트로 돌아갑니다.
  • 배치 스크립트는 이전 Ant 스크립트의 PID를 변수에로드합니다.
  • 배치 스크립트는 내장 된 wmic 도구를 사용하여 Ant가 생성 한 프로세스를 식별합니다.
  • 배치 스크립트는 기본 제공 taskkill 도구를 사용하여 Ant에서 생성 한 모든 하위 프로세스 (및 손자)를 종료합니다.

의 build.xml

<project name="ant-kill-child-processes" default="run" basedir="."> 
    <target name="run"> 
     <fail unless="antPidFile"/> 
     <exec executable="jps"> 
      <!-- Output the arguments passed to each process's main method. --> 
      <arg value="-m"/> 
      <redirector output="${antPidFile}"> 
       <outputfilterchain> 
        <linecontains> 
         <!-- Match the arguments provided to this Ant script. --> 
         <contains value="Launcher -DantPidFile=${antPidFile}"/> 
        </linecontains> 
        <tokenfilter> 
         <!-- The output of the jps command follows the following pattern: --> 
         <!-- lvmid [ [ classname | JARfilename | "Unknown"] [ arg* ] [ jvmarg* ] ] --> 
         <!-- We want the "lvmid" at the beginning of the line. --> 
         <replaceregex pattern="^(\d+).*$" replace="\1"/> 
        </tokenfilter> 
       </outputfilterchain> 
      </redirector> 
     </exec> 
     <!-- As a test, spawn notepad. It will persist after this Ant script exits. --> 
     <exec executable="notepad" spawn="true"/> 
    </target> 
</project> 

배치 스크립트

setlocal 

set DeadAntProcessIdFile=ant-pid.txt 

call ant "-DantPidFile=%DeadAntProcessIdFile%" 

rem The Ant script should have written its PID to DeadAntProcessIdFile. 
set /p DeadAntProcessId=< %DeadAntProcessIdFile% 

rem Kill any lingering processes created by the Ant script. 
for /f "skip=1 usebackq" %%h in (
    `wmic process where "ParentProcessId=%DeadAntProcessId%" get ProcessId ^| findstr .` 
) do taskkill /F /T /PID %%h 
+0

나는 사용자가 명령 행 창을 닫을 경우 배치 스크립트 실행이 계속 생각하지 않습니다. 흥미로운 접근법, 고마워요! – DevCybran

관련 문제