tag:blogger.com,1999:blog-6406593750327945950.post2447797443819124233..comments2022-12-04T20:47:13.182-08:00Comments on Igor Minar's Blog: How a Java Application Can Discover its Process ID (PID)Igor Minarhttp://www.blogger.com/profile/03520548417275543432noreply@blogger.comBlogger32125tag:blogger.com,1999:blog-6406593750327945950.post-57186033639560155602011-10-01T15:59:01.768-07:002011-10-01T15:59:01.768-07:00I found a (hackish and fun) way using the JVM atta...I found a (hackish and fun) way using the JVM attach API, you can read more about it there : https://github.com/vietj/PIDJulien Viethttps://www.blogger.com/profile/04710381063905327936noreply@blogger.comtag:blogger.com,1999:blog-6406593750327945950.post-52204028143243031342011-09-05T06:42:47.126-07:002011-09-05T06:42:47.126-07:00The latest I discovered is the system property sun...The latest I discovered is the system property sun.java.launcher.pid which is set on at least linux platforms JDKs. I haven't tried other Unix. It is not set on Windows. I was going to query that system property and if it isn't set go to the defacto of looking at the JMX Bean.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6406593750327945950.post-59284726295257664912011-06-28T20:16:31.031-07:002011-06-28T20:16:31.031-07:00The 6th way is useless unless you know the pid bef...The 6th way is useless unless you know the pid before you run it which brings us full circle. jvisualvm uses the RuntimeMXBean.getName() method to find a list of vms to attach to and it must parse the pid out of those names. This seems to be the defacto standard.<br />By the way, All machines have a pid for each process. Otherwise they couldn't track processes at all. The difference is whether they expose them.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6406593750327945950.post-65242153356244376012010-11-17T04:00:21.061-08:002010-11-17T04:00:21.061-08:00Hi, another useful approach may be by using JNA(Ja...Hi, another useful approach may be by using JNA(Java Native Access) library (https://jna.dev.java.net/)<br /><br />It is, however platform dependent. If you work under M$ all code is:<br /><br />Kernel32.INSTANCE.GetCurrentProcessId();<br /><br />If you work under UNIX, the code will be different and not as simple, but JNA allows you to use native libraries quite easily under most OS.Unknownhttps://www.blogger.com/profile/04733595907944666028noreply@blogger.comtag:blogger.com,1999:blog-6406593750327945950.post-44187056708009853842010-05-16T03:46:08.599-07:002010-05-16T03:46:08.599-07:00Excellent find Laurent. Thanks for sharing.Excellent find Laurent. Thanks for sharing.Igor Minarhttps://www.blogger.com/profile/03520548417275543432noreply@blogger.comtag:blogger.com,1999:blog-6406593750327945950.post-12783058128541511242010-05-15T05:02:39.297-07:002010-05-15T05:02:39.297-07:00Hi Igor,
I think I've found a 6^th way. It...Hi Igor,<br /><br />I think I've found a 6^th way. It's based on the Attach API that didn't exist at the time you posted this entry. "Attach API"<br />http://blogs.sun.com/CoreJavaTechTips/entry/the_attach_api <br />is like a Java API to list Java processes like JPS does. In addition, one may attach to JVMs, query for system properties and load Java agents. The ``VirtualMachineDescriptor`` really looks like a String-based PID. By now it's a Sun (mean: Oracle) specific API. This doesn't mean other JVM don't support it.<br /><br />That's extremely cool stuff when it comes to kill JVMs spawned with ``ProcessBuilder``. After tagging them with a special system property, you can shut them down gently by loading an agent that calls ``System.exit``. At least it's supposed to be cross-platform (disclaimer: I didn't try it for now).<br /><br />Cheers,<br /><br />Laurent Caillette<br /><br />Mail written in NovelangLaurent Caillettehttp://novelang.sf.netnoreply@blogger.comtag:blogger.com,1999:blog-6406593750327945950.post-79592449692648619062010-05-11T03:13:54.542-07:002010-05-11T03:13:54.542-07:00Cool Brett, I'm glad that it was useful. Too b...Cool Brett, I'm glad that it was useful. Too bad that even after 3 years, there is no hint from Sun/Oracle that we'll ever get a proper api to do this.Igor Minarhttps://www.blogger.com/profile/03520548417275543432noreply@blogger.comtag:blogger.com,1999:blog-6406593750327945950.post-24723655661033960922010-05-10T16:31:57.564-07:002010-05-10T16:31:57.564-07:00nice summary Igor.
I'm using glassfish v3 an...nice summary Igor. <br /><br />I'm using glassfish v3 and really wanted the java process to write its pid.<br /><br />so I wired up a an osgi Activator to write pidfile on start.Bretthttps://www.blogger.com/profile/15876213791801206384noreply@blogger.comtag:blogger.com,1999:blog-6406593750327945950.post-50975339496403813582009-12-25T16:40:54.771-08:002009-12-25T16:40:54.771-08:00Hey Igor
You can also check out my project for mo...Hey Igor<br /><br />You can also check out my project for monitoring performance in Java. I just added a call for pid: <a href="http://bit.ly/javasysmon" rel="nofollow">JavaSysMon at GitHub</a>Unknownhttps://www.blogger.com/profile/10321354226835044785noreply@blogger.comtag:blogger.com,1999:blog-6406593750327945950.post-65993844577626590452009-11-12T21:23:13.653-08:002009-11-12T21:23:13.653-08:00Hi all,
I tried the reflection technique , it wor...Hi all,<br /><br />I tried the reflection technique , it works only on linux.<br /><br />We get 'java.lang.NoSuchFieldException: pid'<br />on windows.<br /><br />with regards,<br />ch vishnu213vishnuraohttps://www.blogger.com/profile/13865191899113945498noreply@blogger.comtag:blogger.com,1999:blog-6406593750327945950.post-71082806521121998602009-09-09T11:32:58.278-07:002009-09-09T11:32:58.278-07:00jps is the best way to find a pid for an external ...jps is the best way to find a pid for an external jvm process. The methods I captured in my blog post deal with discovering the process pid from within the java application.Igor Minarhttps://www.blogger.com/profile/03520548417275543432noreply@blogger.comtag:blogger.com,1999:blog-6406593750327945950.post-36964820574292505922009-09-09T10:00:13.456-07:002009-09-09T10:00:13.456-07:00One more method -- exec jps (available in all JDKs...One more method -- exec jps (available in all JDKs) and find the pid by the classname.<br /><br />What nonsense this is!!<br /><br />As to the argument of there maybe being an OS that does not use pid -- no problem. Return -1 in that case! Why should we use the lowest common denominator?Byron Nevinshttps://www.blogger.com/profile/06536180236748376172noreply@blogger.comtag:blogger.com,1999:blog-6406593750327945950.post-80554567153748902692009-09-02T08:59:25.144-07:002009-09-02T08:59:25.144-07:00Keep in mind that $PPID returns the pid of the par...Keep in mind that $PPID returns the pid of the parent process. So it won't return the process of the newly spawned process, but rather the pid of its parent, which is the JVM process.Igor Minarhttps://www.blogger.com/profile/03520548417275543432noreply@blogger.comtag:blogger.com,1999:blog-6406593750327945950.post-82705173625404474592009-09-02T02:11:14.333-07:002009-09-02T02:11:14.333-07:00If the JVM starts a new bash process, wouldn't...If the JVM starts a new bash process, wouldn't the $PPID returned be the pid of the bash process, rather than the pid of the JVM?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6406593750327945950.post-16906627619063855262009-08-10T15:22:14.372-07:002009-08-10T15:22:14.372-07:00The program above prints different values for a PI...The program above prints different values for a PID because... well... they are really different process IDs!<br /><br />peter@peter:~/workspace/pidtest$ java -jar ./pidtest.jar<br />5539<br />5541<br />5543<br />5545<br />5547<br />5549<br />5551<br />5553<br />5555<br />5557<br /><br />On Unix systems, every time an image is loaded into memory, a new process is created.<br /><br />When you run ...<br /><br />Process p = Runtime.getRuntime ().exec (cmd);<br /><br />... several times, you are creating several processes and you should expect different PIDs. This is perfectly normal.<br /><br />Richard Gomes<br />http://www.jquantlib.org/index.php/User:RichardGomes<br />twitter: frgomes<br /><br />JQuantlib is a Library for Quantitative Finance written in Java.<br />http://www.jquantlib.org/<br />twitter: jquantlibAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-6406593750327945950.post-88528658597756956442009-07-30T03:06:07.397-07:002009-07-30T03:06:07.397-07:00Thanx dude... really helped me out!!Thanx dude... really helped me out!!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6406593750327945950.post-51038259515123665642009-03-03T08:24:00.000-08:002009-03-03T08:24:00.000-08:00Excellent overview. I think the Java management AP...Excellent overview. I think the Java management API is the best approach, assuming your JVM is 1.5 or higher. I've used it on Windows XP (Sun JVM 1.5) and AIX 5.2 (IBM JVM 1.5).Unknownhttps://www.blogger.com/profile/02744452573753284598noreply@blogger.comtag:blogger.com,1999:blog-6406593750327945950.post-48931113070080689912009-02-20T09:13:00.000-08:002009-02-20T09:13:00.000-08:00worked perfectly on linux x86:12766@hostnameworked perfectly on linux x86:<BR/>12766@hostnameRoger Packhttps://www.blogger.com/profile/01578246846716577925noreply@blogger.comtag:blogger.com,1999:blog-6406593750327945950.post-33585759175175319692008-08-05T15:23:00.000-07:002008-08-05T15:23:00.000-07:00Hi, Igor -- you have a beautiful talent for unders...Hi, Igor -- you have a beautiful talent for understatement! ("not pretty", indeed!)<BR/><BR/>Is there something like /proc/self for Solaris?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6406593750327945950.post-57969590625781971872008-08-04T09:58:00.000-07:002008-08-04T09:58:00.000-07:00Hi Charles,your solution is not pretty, but hey in...Hi Charles,<BR/><BR/>your solution is not pretty, but hey in this case it's better than nothing ;-)<BR/><BR/>/iIgor Minarhttps://www.blogger.com/profile/03520548417275543432noreply@blogger.comtag:blogger.com,1999:blog-6406593750327945950.post-21412668143994663832008-08-04T09:38:00.000-07:002008-08-04T09:38:00.000-07:00Igor -- thanks for starting this.I wanted to find ...Igor -- thanks for starting this.<BR/><BR/>I wanted to find out not only the process id of the entire process, but also the thread id (aka "lightweight process id") of new threads.<BR/><BR/>I found a kluge that works, most of the time, under linux -- described in detail at http://thedance.net/~roth/TECHBLOG/threadtracker.html<BR/><BR/>Basically it involves reading /proc/self/task multiple times to look for new threads as they get created. This is linux-only, but could possibly be adapted to other *nixes.<BR/><BR/>Cheers!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6406593750327945950.post-62802978928188284792008-07-02T12:04:00.000-07:002008-07-02T12:04:00.000-07:00I found that the method retrieving name from runti...I found that the method retrieving name from runtime mbean works fine under Linux and Mac OS X, for Windows I have not an test enviroment.<BR/>Great solution, Thanks a lot.<BR/><BR/>JanAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-6406593750327945950.post-14047694246182106052008-06-30T03:38:00.000-07:002008-06-30T03:38:00.000-07:00Excellent man. The reflection solution works prett...Excellent man. The reflection solution works pretty good and thanks a lot.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6406593750327945950.post-15488742826797253782007-10-19T10:15:00.000-07:002007-10-19T10:15:00.000-07:00Reflection will also work. On UNIX the subclass of...Reflection will also work. On UNIX the subclass of Process has a private field with the PID that the default JVM security settings allow you to get via reflection:<BR/><BR/> static int getPID(Process process)<BR/> throws IllegalAccessException, IllegalArgumentException,<BR/> NoSuchFieldException, SecurityException<BR/> {<BR/> Field field = process.getClass().getDeclaredField("pid");<BR/> field.setAccessible(true);<BR/> return field.getInt(process);<BR/> }Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6406593750327945950.post-47526427427658085822007-07-31T03:26:00.000-07:002007-07-31T03:26:00.000-07:00Dear John,very many thanks for that solution! It w...Dear John,<BR/><BR/>very many thanks for that solution! It works excellently! I hope is that this method is portable across Linux distributions and here to stay for the future. Forgive my ignorance on this...<BR/><BR/>I have tried the method with a test program using the proposed code and got the following output:<BR/><BR/>18810<BR/>18810<BR/>18810<BR/>18810<BR/>18810<BR/>18810<BR/>18810<BR/>18810<BR/>18810<BR/>18810<BR/><BR/>/*******************************************************************************<BR/> * tpidtest.java <BR/> * ------------------- <BR/> * initial author : Peter Hoppe <BR/> * copyright : (C) 2007 <BR/> ******************************************************************************/<BR/><BR/>/*******************************************************************************<BR/> * <BR/> * This program is free software; you can redistribute it and/or modify it under<BR/> * the terms of the GNU General Public License as published by the Free Software<BR/> * Foundation; either version 3 of the License, or (at your option) any later<BR/> * version.<BR/> * <BR/> * This program is distributed in the hope that it will be useful, but WITHOUT <BR/> * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or <BR/> * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for <BR/> * more details.<BR/> *<BR/> * You should have received a copy of the GNU General Public License along with<BR/> * this program; if not, write to the Free Software Foundation, Inc., 675 Mass<BR/> * Ave, Cambridge, MA 02139, USA.<BR/> * <BR/> ******************************************************************************/<BR/><BR/><BR/>package org.rww.mmailstarter.test;<BR/><BR/>import java.io.File;<BR/>import java.io.IOException;<BR/><BR/>/**<BR/> * Prints (to stdout) the PID of the java process that runs this program.<BR/> * <BR/> * The PID discovery method is specific to the Linux Operating system and <BR/> * may not work on other operating systems.<BR/> */<BR/>public class TPidTest<BR/>{<BR/> public static void main (String [] args)<BR/> {<BR/> int i = 0;<BR/> int pid = 0;<BR/> <BR/> try<BR/> {<BR/> for (i = 0; i < 10; i++)<BR/> {<BR/> pid = Integer.parseInt( ( new File("/proc/self")).getCanonicalFile().getName() );<BR/> System.out.println (pid);<BR/> }<BR/> }<BR/> catch (NumberFormatException e)<BR/> {<BR/> e.printStackTrace();<BR/> }<BR/> catch (IOException e)<BR/> {<BR/> e.printStackTrace();<BR/> }<BR/> }<BR/>}Anonymousnoreply@blogger.com