5

I have an ant task, and within it I'd like to get the current process id (a la echo $PPID from command line).

I'm running ksh on Solaris, so I thought I could just do this:

<property environment="env" />
<target name="targ">
    <echo message="PID is ${env.PPID}" />
    <echo message="PID is ${env.$$}" />
</target>

But that didn't work; the variables aren't substituted. Turns out PPID, SECONDS, and certain other env variables don't make it into Ant's representation.

Next I try this:

<target name="targ">
    <exec executable="${env.pathtomyfiles}/getpid.sh" />
</target>

getpid.sh looks like this:

echo $$

This gets me the PID of the spawned shell script. Closer, but not really what I need.

I just want my current process ID, so I can make a temporary file with that value in the name. Any thoughts?

AlBlue
  • 23,254
  • 14
  • 71
  • 91
akaioi
  • 265
  • 4
  • 13

3 Answers3

7

You can find PID using java process monitoring tool JPS, then output stream can be filtered and if needed process can be killed. check out this tomcat pid kill script:

<target name="tomcat.kill" depends="tomcat.shutdown">
  <exec executable="jps">
    <arg value="-l"/>
    <redirector outputproperty="process.pid">
        <outputfilterchain>
            <linecontains>
              <contains value="C:\tomcat\tomcat_node5\bin\bootstrap.jar"/>
            </linecontains>
            <replacestring from=" C:\tomcat\tomcat_node5\bin\bootstrap.jar"/>
        </outputfilterchain>
    </redirector>
  </exec>
  <exec executable="taskkill" osfamily="winnt">
    <arg value="/F"/>
    <arg value="/PID"/>
    <arg value="${process.pid}"/>
  </exec>
  <exec executable="kill" osfamily="unix">
    <arg value="-9"/>
    <arg value="${process.pid}"/>
  </exec>
</target>
  • JPS is only will give process Id of any java process. How to deal with non-java process.. Example how do i find cmd.exe process ID. on "C:\test\bin" path – Mr.Pramod Anarase Jul 14 '15 at 09:55
  • This worked like a charm. You don't need to pass -l in necessarily. It can make your search and replace easier. – David Brossard Feb 20 '19 at 22:52
3

Why not just use the tempfile Ant task, instead? It does what you really want to do, while hiding all the gory details.

See http://ant.apache.org/manual/Tasks/tempfile.html.

martin clayton
  • 76,436
  • 32
  • 213
  • 198
Brian Clapper
  • 25,705
  • 7
  • 65
  • 65
1

your second method doesn't get ANT's pid. Change the shell script to (I use bash, I don't know if ksh is the same):

echo "$PPID"
jscoot
  • 2,019
  • 3
  • 24
  • 27