Re: [OT] How to obtain the id of the current process
On 19/11/2010 19:55, André Warnier wrote: Like that JVM's JMX port for instance ? That's not a bad idea. If JMX was configured using the new Listener in server.xml and the port set in catalina.properties, then you'd be able to find out the port number without having to parse XML etc. p 0x62590808.asc Description: application/pgp-keys signature.asc Description: OpenPGP digital signature
Re: [OT] How to obtain the id of the current process
On Thu, Nov 18, 2010 at 6:11 PM, André Warnier a...@ice-sa.com wrote: Christopher Schultz wrote: Maybe we should turn the problem around though. If Leon wanted the PID, it was obviously to do something with it later. What do you do with a PID ? Usually, one uses it to send a signal to a process. actually in the very first line, i wanted to find out if an instance of the process was already running. :-) regards Leon And sending a signal to a process, Unix-like, is not likely to be very multi-platform in the first place. So maybe finding a purely Java-based alternative to do what Leon wants to do with the PID would be more productive in the long run ? - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: [OT] How to obtain the id of the current process
Leon Rosenberg wrote: On Thu, Nov 18, 2010 at 6:11 PM, André Warnier a...@ice-sa.com wrote: Christopher Schultz wrote: Maybe we should turn the problem around though. If Leon wanted the PID, it was obviously to do something with it later. What do you do with a PID ? Usually, one uses it to send a signal to a process. actually in the very first line, i wanted to find out if an instance of the process was already running. Ha. So maybe you could just check if some well-known port is open for business ? Like that JVM's JMX port for instance ? That would even have the advantages that it works at a distance, and is really multi-platform. - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: [OT] How to obtain the id of the current process
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 André, On 11/18/2010 7:03 PM, André Warnier wrote: André Warnier wrote: Christopher Schultz wrote: You also might want to kill it. In either case, most JVMs are running on win32 or UNIX-like OSs, and they both support signals. Win32 supports signals ? Well yes, kind of, apparently : http://msdn.microsoft.com/en-us/library/xdkz3x12%28VS.71%29.aspx Yes, kind of. ;) - -chris -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.10 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAkznA7sACgkQ9CaO5/Lv0PDy5QCeLP7SNLjzvrF1uyTKh8FixbqP DsQAmwTDkvMf6mb356hjh1XS8Z0+F5Pp =81u/ -END PGP SIGNATURE- - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: [OT] How to obtain the id of the current process
On 17/11/2010 22:41, André Warnier wrote: Typical Java thinking.. Sure, let's pull in another 15 classes.. Gotta use those GB of RAM for something.. Why do simple when complicated would do just as well, he ? That doesn't make sense*. Leon's trying to avoid using more RAM. The JMX method pulls in one class, uses another indirectly generates a String. Hardly a memory intensive operation. Splitting a String isn't exactly elegant, but it _is_ cross platform AFAICT starting processes appears to be problematic, so it's the best of a bad bunch IMHO. p * But I know it's an excuse for a reply with a Perl solution ;) 0x62590808.asc Description: application/pgp-keys signature.asc Description: OpenPGP digital signature
Re: [OT] How to obtain the id of the current process
Pid wrote: On 17/11/2010 22:41, André Warnier wrote: Typical Java thinking.. Sure, let's pull in another 15 classes.. Gotta use those GB of RAM for something.. Why do simple when complicated would do just as well, he ? That doesn't make sense*. Leon's trying to avoid using more RAM. The JMX method pulls in one class, uses another indirectly generates a String. Hardly a memory intensive operation. Splitting a String isn't exactly elegant, but it _is_ cross platform AFAICT starting processes appears to be problematic, so it's the best of a bad bunch IMHO. I think you missed the ;-) which was part of the jibe... This being said, I agree that this JMX method seems to be the only cross-platform one, among the 10 or so solutions seen so far (some of which are really resource-intensive, as I pointed out). The real question here is why there is not a *simple* direct method call available in the JVM. The justification according to which a PID is not necessarily available on every platform seems a very weak one. - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: [OT] How to obtain the id of the current process
On 18/11/2010 10:47, André Warnier wrote: The justification according to which a PID is not necessarily available on every platform seems a very weak one. Quite. In which case, return a null. Enough other methods do... p 0x62590808.asc Description: application/pgp-keys signature.asc Description: OpenPGP digital signature
Re: [OT] How to obtain the id of the current process
If you have Apr available ... you might be able to use this ... org.apache.tomcat.jni.Stdlib.getpid() // public static native int getpid(); -Tim On 11/18/2010 6:10 AM, Pid wrote: On 18/11/2010 10:47, André Warnier wrote: The justification according to which a PID is not necessarily available on every platform seems a very weak one. Quite. In which case, return a null. Enough other methods do... p - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: [OT] How to obtain the id of the current process
That is why my Tomcats spawn a pool of sh scripts to have them run a unix command (ImageMagick, etc.) Forking of a threaded process is very expensive on a loaded server. Unix has to suspend all threads, than fork, do a lot of housekeeping and than resume all threads. After making a pool of running sh instances which listen on stdin for commands my system %cpu went down so it has more time for user %cpu. Throughput doubled in my scenario's. Ronald. Op woensdag, 17 november 2010 22:30 schreef Leon Rosenberg rosenberg.l...@gmail.com: Hi, I have a ugly issue I'm sure many people on this list already solved. For multiple purposes I need pid's of processes, to stop/start them or ensure they are running. So far nothing spectacular. To obtain the pid I have following util: String[] cmd = { /bin/bash, -c, echo $PPID }; p = Runtime.getRuntime().exec(cmd); and afterwards read the ppid from the output stream of my process. So far - so good. The problem now occurs if a process has a lot of memory configured. Like 25Gb on a 32Gb machine. In such a configuration the above command fails with an OS'es out of memory, because fork fails to create another process with THAT amount of memory: 1598388 2010-11-17 02:54:47,061 ERROR net.anotheria.util.PidTools - Could not determine PID: java.io.IOException: Cannot run program /bin/bash: java.io.IOException: error=12, Cannot allocate memory Is there another method to obtain the process id from within the process? regards Leon - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: [OT] How to obtain the id of the current process
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 André, On 11/17/2010 4:50 PM, André Warnier wrote: I found the following trick somewhere, maybe it works for you : When starting your JVM, use a line like java -Dpid=$$ program.java and in the java program using the statement System.getProperty(pid); If it works, it's cute, and certainly a lot less overhead. Doesn't that set the pid system property to the pid of the shell that launched the JVM? - -chris -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.10 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAkzlQjkACgkQ9CaO5/Lv0PC63ACfROqppZe+t4kKOuEq/r4PeQDL md8AnA2fvxwI89iHlm4wjdEx/kbvPpHZ =Ghld -END PGP SIGNATURE- - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: [OT] How to obtain the id of the current process
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Tim, On 11/18/2010 7:19 AM, Tim Funk wrote: If you have Apr available ... you might be able to use this ... org.apache.tomcat.jni.Stdlib.getpid() // public static native int getpid(); I was going to suggest roughly the same thing, except I didn't know it was available via APR. The only really scary thing about JNI is having to implement your shared library for all potential platforms. If you have one environment for dev/test/prod, then you're all good: just create one library. - -chris -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.10 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAkzlQ2UACgkQ9CaO5/Lv0PAHVACfb6ur/Yl3tyihP8cZ8r3sHzJx 4nEAoIuXSctW9rieXZwajMXf5fw0aSSI =qOLG -END PGP SIGNATURE- - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: [OT] How to obtain the id of the current process
Christopher Schultz wrote: -BEGIN PGP SIGNED MESSAGE- Hash: SHA1 André, On 11/17/2010 4:50 PM, André Warnier wrote: I found the following trick somewhere, maybe it works for you : When starting your JVM, use a line like java -Dpid=$$ program.java and in the java program using the statement System.getProperty(pid); If it works, it's cute, and certainly a lot less overhead. Doesn't that set the pid system property to the pid of the shell that launched the JVM? Now that you mention it, I think so too. But that may be what Leon, the OP wanted : the top parent of the process running the JVM (and Tomcat). I suppose it would depend on what exactly you want to do with it. What happens when you kill the shell which runs the JVM which runs Tomcat ? Another question is whether it would be possible to delay the interpretation of the $$ until such time as when the JVM looks at it. Something like : java -Dpid='$$' ... (is there a way to tell java to get the value of an environment variable when it starts running ? I thought there was something of the kind. But maybe that variable is not set then. I am a bit confused now.) Maybe we should turn the problem around though. If Leon wanted the PID, it was obviously to do something with it later. What do you do with a PID ? Usually, one uses it to send a signal to a process. And sending a signal to a process, Unix-like, is not likely to be very multi-platform in the first place. So maybe finding a purely Java-based alternative to do what Leon wants to do with the PID would be more productive in the long run ? - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: [OT] How to obtain the id of the current process
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 André, On 11/18/2010 12:11 PM, André Warnier wrote: Christopher Schultz wrote: -BEGIN PGP SIGNED MESSAGE- Hash: SHA1 André, On 11/17/2010 4:50 PM, André Warnier wrote: I found the following trick somewhere, maybe it works for you : When starting your JVM, use a line like java -Dpid=$$ program.java and in the java program using the statement System.getProperty(pid); If it works, it's cute, and certainly a lot less overhead. Doesn't that set the pid system property to the pid of the shell that launched the JVM? Now that you mention it, I think so too. Something that looks suspiciously like the above, but would probably work is this: (exec java -Dpid=$$ program.java) Since the shell (well, bash anyway) will execute the expression in parens within a child process and then the 'exec' will replace the process with another, the $$ magic works. Another question is whether it would be possible to delay the interpretation of the $$ until such time as when the JVM looks at it. Something like : java -Dpid='$$' ... (is there a way to tell java to get the value of an environment variable when it starts running ? I thought there was something of the kind. But maybe that variable is not set then. I am a bit confused now.) $$ is entirely a shell notion: other processes wouldn't know what $$ meant unless they were specifically programmed to. If the JVM were one of those things, you'd expect that you could call System.getCurrentProcessId or something like that. :( Maybe we should turn the problem around though. If Leon wanted the PID, it was obviously to do something with it later. What do you do with a PID ? Usually, one uses it to send a signal to a process. And sending a signal to a process, Unix-like, is not likely to be very multi-platform in the first place. So maybe finding a purely Java-based alternative to do what Leon wants to do with the PID would be more productive in the long run ? +1 You also might want to kill it. In either case, most JVMs are running on win32 or UNIX-like OSs, and they both support signals. - -chris -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.10 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAkzlr00ACgkQ9CaO5/Lv0PDBjgCguhUMErvQ+pUyNZ56whBiQC9W uUMAoKiRheNTWp4xFAPB2IPFhK71Iion =bReB -END PGP SIGNATURE- - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: [OT] How to obtain the id of the current process
Christopher Schultz wrote: You also might want to kill it. In either case, most JVMs are running on win32 or UNIX-like OSs, and they both support signals. Win32 supports signals ? - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: [OT] How to obtain the id of the current process
André Warnier wrote: Christopher Schultz wrote: You also might want to kill it. In either case, most JVMs are running on win32 or UNIX-like OSs, and they both support signals. Win32 supports signals ? Well yes, kind of, apparently : http://msdn.microsoft.com/en-us/library/xdkz3x12%28VS.71%29.aspx - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
[OT] How to obtain the id of the current process
Hi, I have a ugly issue I'm sure many people on this list already solved. For multiple purposes I need pid's of processes, to stop/start them or ensure they are running. So far nothing spectacular. To obtain the pid I have following util: String[] cmd = { /bin/bash, -c, echo $PPID }; p = Runtime.getRuntime().exec(cmd); and afterwards read the ppid from the output stream of my process. So far - so good. The problem now occurs if a process has a lot of memory configured. Like 25Gb on a 32Gb machine. In such a configuration the above command fails with an OS'es out of memory, because fork fails to create another process with THAT amount of memory: 1598388 2010-11-17 02:54:47,061 ERROR net.anotheria.util.PidTools - Could not determine PID: java.io.IOException: Cannot run program /bin/bash: java.io.IOException: error=12, Cannot allocate memory Is there another method to obtain the process id from within the process? regards Leon - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: [OT] How to obtain the id of the current process
On 17/11/2010 21:30, Leon Rosenberg wrote: Hi, I have a ugly issue I'm sure many people on this list already solved. For multiple purposes I need pid's of processes, to stop/start them or ensure they are running. So far nothing spectacular. To obtain the pid I have following util: String[] cmd = { /bin/bash, -c, echo $PPID }; p = Runtime.getRuntime().exec(cmd); and afterwards read the ppid from the output stream of my process. So far - so good. The problem now occurs if a process has a lot of memory configured. Like 25Gb on a 32Gb machine. In such a configuration the above command fails with an OS'es out of memory, because fork fails to create another process with THAT amount of memory: 1598388 2010-11-17 02:54:47,061 ERROR net.anotheria.util.PidTools - Could not determine PID: java.io.IOException: Cannot run program /bin/bash: java.io.IOException: error=12, Cannot allocate memory Is there another method to obtain the process id from within the process? I'm not sure I entirely understand the context, but if it's a Java 5+ process: ManagementFactory.getRuntimeMXBean().getName() will return pid@hostname. Are you trying to determine the PID of a new process, or the current one? p regards Leon - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org 0x62590808.asc Description: application/pgp-keys signature.asc Description: OpenPGP digital signature
Re: [OT] How to obtain the id of the current process
I found the following trick somewhere, maybe it works for you : When starting your JVM, use a line like java -Dpid=$$ program.java and in the java program using the statement System.getProperty(pid); If it works, it's cute, and certainly a lot less overhead. Credits : http://www.jguru.com/faq/view.jsp?EID=416212 (last one) Leon Rosenberg wrote: Hi, I have a ugly issue I'm sure many people on this list already solved. For multiple purposes I need pid's of processes, to stop/start them or ensure they are running. So far nothing spectacular. To obtain the pid I have following util: String[] cmd = { /bin/bash, -c, echo $PPID }; p = Runtime.getRuntime().exec(cmd); and afterwards read the ppid from the output stream of my process. So far - so good. The problem now occurs if a process has a lot of memory configured. Like 25Gb on a 32Gb machine. In such a configuration the above command fails with an OS'es out of memory, because fork fails to create another process with THAT amount of memory: 1598388 2010-11-17 02:54:47,061 ERROR net.anotheria.util.PidTools - Could not determine PID: java.io.IOException: Cannot run program /bin/bash: java.io.IOException: error=12, Cannot allocate memory Is there another method to obtain the process id from within the process? regards Leon - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: [OT] How to obtain the id of the current process
Pid, Andre thanx. I will try both approaches. I will try the MX Bean first, this just seems to be more elegant ;) thank you Leon On Wed, Nov 17, 2010 at 10:50 PM, André Warnier a...@ice-sa.com wrote: I found the following trick somewhere, maybe it works for you : When starting your JVM, use a line like java -Dpid=$$ program.java and in the java program using the statement System.getProperty(pid); If it works, it's cute, and certainly a lot less overhead. Credits : http://www.jguru.com/faq/view.jsp?EID=416212 (last one) Leon Rosenberg wrote: Hi, I have a ugly issue I'm sure many people on this list already solved. For multiple purposes I need pid's of processes, to stop/start them or ensure they are running. So far nothing spectacular. To obtain the pid I have following util: String[] cmd = { /bin/bash, -c, echo $PPID }; p = Runtime.getRuntime().exec(cmd); and afterwards read the ppid from the output stream of my process. So far - so good. The problem now occurs if a process has a lot of memory configured. Like 25Gb on a 32Gb machine. In such a configuration the above command fails with an OS'es out of memory, because fork fails to create another process with THAT amount of memory: 1598388 2010-11-17 02:54:47,061 ERROR net.anotheria.util.PidTools - Could not determine PID: java.io.IOException: Cannot run program /bin/bash: java.io.IOException: error=12, Cannot allocate memory Is there another method to obtain the process id from within the process? regards Leon - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: [OT] How to obtain the id of the current process
2010/11/18 Leon Rosenberg rosenberg.l...@gmail.com: Is there another method to obtain the process id from within the process? Of Tomcat process, or of something else? You can set $CATALINA_PID in your bin/setenv.sh, and Tomcat's PID will be written to that file. If you need it for something else, ... these are the two first results for 'java process PID' in Google:: [1] http://bugs.sun.com/view_bug.do?bug_id=4244896 (an RFE asking for tis feature, you can vote for it) [2] http://blog.igorminar.com/2007/03/how-java-application-can-discover-its.html Best regards, Konstantin Kolinko - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: [OT] How to obtain the id of the current process
On 17/11/2010 22:02, Konstantin Kolinko wrote: 2010/11/18 Leon Rosenberg rosenberg.l...@gmail.com: Is there another method to obtain the process id from within the process? Of Tomcat process, or of something else? You can set $CATALINA_PID in your bin/setenv.sh, and Tomcat's PID will be written to that file. If you need it for something else, ... these are the two first results for 'java process PID' in Google:: [1] http://bugs.sun.com/view_bug.do?bug_id=4244896 (an RFE asking for tis feature, you can vote for it) [2] http://blog.igorminar.com/2007/03/how-java-application-can-discover-its.html So the first request for PID method was ~1999? This is great progress. But I guess it's probably more important to add Lambdas than to add a useful method call to the language. :s p Best regards, Konstantin Kolinko - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org 0x62590808.asc Description: application/pgp-keys signature.asc Description: OpenPGP digital signature
Re: [OT] How to obtain the id of the current process
Leon Rosenberg wrote: Pid, Andre thanx. I will try both approaches. I will try the MX Bean first, this just seems to be more elegant ;) Typical Java thinking.. Sure, let's pull in another 15 classes.. Gotta use those GB of RAM for something.. Why do simple when complicated would do just as well, he ? :-) While you're at it, here is another variation of the original scheme, much more to my own liking : String[] cmd = { /usr/bin/perl, -e, print getppid(),\\\n\; }; p = Runtime.getRuntime().exec(cmd); Same defect as yours though, damn. Or (from the same jguru post) : String[] cmd = { /bin/sh, -c, /bin/ps -f | /bin/awk '{print $2,$3}' | /bin/grep \^$$\ | /bin/awk '{print $2}' }; p = Runtime.getRuntime().exec(cmd); Now that one is not really elegant, but kind of .. macho ! 5 processes in a row, with pipes. Woaw. To get serious again, it /is/ funny that Java does not provide this simple function. And somehow I have a feeling that there may be a catch somewhere when dealing with threads under various platforms. I prefer Konstantin's first suggestion. Now the question is : if Tomcat writes it's PID to that file, where does Tomcat get it from ? thank you Leon On Wed, Nov 17, 2010 at 10:50 PM, André Warnier a...@ice-sa.com wrote: I found the following trick somewhere, maybe it works for you : When starting your JVM, use a line like java -Dpid=$$ program.java and in the java program using the statement System.getProperty(pid); If it works, it's cute, and certainly a lot less overhead. Credits : http://www.jguru.com/faq/view.jsp?EID=416212 (last one) Leon Rosenberg wrote: Hi, I have a ugly issue I'm sure many people on this list already solved. For multiple purposes I need pid's of processes, to stop/start them or ensure they are running. So far nothing spectacular. To obtain the pid I have following util: String[] cmd = { /bin/bash, -c, echo $PPID }; p = Runtime.getRuntime().exec(cmd); and afterwards read the ppid from the output stream of my process. So far - so good. The problem now occurs if a process has a lot of memory configured. Like 25Gb on a 32Gb machine. In such a configuration the above command fails with an OS'es out of memory, because fork fails to create another process with THAT amount of memory: 1598388 2010-11-17 02:54:47,061 ERROR net.anotheria.util.PidTools - Could not determine PID: java.io.IOException: Cannot run program /bin/bash: java.io.IOException: error=12, Cannot allocate memory Is there another method to obtain the process id from within the process? regards Leon - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: [OT] How to obtain the id of the current process
2010/11/18 André Warnier a...@ice-sa.com: I prefer Konstantin's first suggestion. Now the question is : if Tomcat writes it's PID to that file, where does Tomcat get it from ? Look into catalina.sh echo $! $CATALINA_PID (after spawning java as a background process) BTW, from all methods listed in the comments on [2] http://blog.igorminar.com/2007/03/how-java-application-can-discover-its.html I like the following: int pid = Integer.parseInt( ( new File(/proc/self)).getCanonicalFile().getName() ); There are others as well. Best regards, Konstantin Kolinko - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: [OT] How to obtain the id of the current process
On Thu, Nov 18, 2010 at 12:00 AM, Konstantin Kolinko knst.koli...@gmail.com wrote: I like the following: int pid = Integer.parseInt( ( new File(/proc/self)).getCanonicalFile().getName() ); doesn't work on a mac ;-( - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: [OT] How to obtain the id of the current process
On Wed, Nov 17, 2010 at 11:41 PM, André Warnier a...@ice-sa.com wrote: Leon Rosenberg wrote: Pid, Andre thanx. I will try both approaches. I will try the MX Bean first, this just seems to be more elegant ;) Typical Java thinking.. Sure, let's pull in another 15 classes.. Gotta use those GB of RAM for something.. Why do simple when complicated would do just as well, he ? :-) Well it will actually replace one line of code with another. In fact the whole code will be shorter (reading of output etc). While you're at it, here is another variation of the original scheme, much more to my own liking : String[] cmd = { /usr/bin/perl, -e, print getppid(),\\\n\; }; p = Runtime.getRuntime().exec(cmd); Same defect as yours though, damn. Or (from the same jguru post) : String[] cmd = { /bin/sh, -c, /bin/ps -f | /bin/awk '{print $2,$3}' | /bin/grep \^$$\ | /bin/awk '{print $2}' }; p = Runtime.getRuntime().exec(cmd); same fork problem. Now that one is not really elegant, but kind of .. macho ! 5 processes in a row, with pipes. Woaw. To get serious again, it /is/ funny that Java does not provide this simple function. I assume that's because not each of the target platforms supports the concept of a pid ;-) And somehow I have a feeling that there may be a catch somewhere when dealing with threads under various platforms. threads? which threads? I prefer Konstantin's first suggestion. Now the question is : if Tomcat writes it's PID to that file, where does Tomcat get it from ? The problem is, that I don't always run my processes in a tomcat, in fact most of the time they don't (hence the OT). java -Dpid=$$ program.java and in the java program using the statement System.getProperty(pid); this looks nice, but won't work without a script (aka start from eclipse). thank you regards Leon thank you Leon On Wed, Nov 17, 2010 at 10:50 PM, André Warnier a...@ice-sa.com wrote: I found the following trick somewhere, maybe it works for you : When starting your JVM, use a line like java -Dpid=$$ program.java and in the java program using the statement System.getProperty(pid); If it works, it's cute, and certainly a lot less overhead. Credits : http://www.jguru.com/faq/view.jsp?EID=416212 (last one) Leon Rosenberg wrote: Hi, I have a ugly issue I'm sure many people on this list already solved. For multiple purposes I need pid's of processes, to stop/start them or ensure they are running. So far nothing spectacular. To obtain the pid I have following util: String[] cmd = { /bin/bash, -c, echo $PPID }; p = Runtime.getRuntime().exec(cmd); and afterwards read the ppid from the output stream of my process. So far - so good. The problem now occurs if a process has a lot of memory configured. Like 25Gb on a 32Gb machine. In such a configuration the above command fails with an OS'es out of memory, because fork fails to create another process with THAT amount of memory: 1598388 2010-11-17 02:54:47,061 ERROR net.anotheria.util.PidTools - Could not determine PID: java.io.IOException: Cannot run program /bin/bash: java.io.IOException: error=12, Cannot allocate memory Is there another method to obtain the process id from within the process? regards Leon - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org