How can I get the current PID from Ant?

I have an ant task, and inside it I want to get the current process identifier (a la echo $PPID from the command line).

I am 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 did not work; variables are not replaced. Turns off PPID , SECONDS , and some other env variables do not fall into the ant view.

Next I try:

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

getpid.sh as follows:

 echo $$ 

This gives me the PID of the generated shell script. Closer, but not quite what I need.

I just need my current process id, so I can make a temporary file with this value in the name. Any thoughts?

+4
source share
3 answers

You can find the PID using the JSP JSP tool to control the process, then the output stream can be filtered and, if necessary, the 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> 
+5
source

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

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

+3
source

your second method does not receive ANT pid. Change the shell of the script to (I'm using bash, I don't know if ksh is the same):

 echo "$PPID" 
+1
source

All Articles