Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get current PID from within Ant?

Tags:

ant

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?

like image 223
akaioi Avatar asked Jan 12 '09 23:01

akaioi


3 Answers

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>
like image 53
Anton Zukovskij Avatar answered Oct 13 '22 17:10

Anton Zukovskij


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.

like image 22
Brian Clapper Avatar answered Oct 13 '22 17:10

Brian Clapper


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"
like image 1
jscoot Avatar answered Oct 13 '22 18:10

jscoot