Java question (Urgent help needed!)

Hi, I am running a process using the java Runtime class like so. Process p = Runtime.getRuntime().exec("gksu rfcomm connect 0 0B:D4:91:39:66:01"); I now want to get the process ID of process p that is returned in the above statement. I have tried using: Field field = p.getClass().getDeclaredField("pid"); field.setAccessible(true); this.pid=field.getInt(p); //this.pid is an int value System.out.println("process is running +"+pid); but the pid that is printed does not match what I get when I run the ps tool in the shell. It is important that they match because I want to run a Process p = Runtime.getRuntime().exec("gksu kill -9 "+pid); when I am done with the rfcomm tool. ALTERNATIVELY: Someone please tell me how to send a CTRL-C signal after I have run the Process p = Runtime.getRuntime().exec("gksu rfcomm connect 0 0B:D4:91:39:66:01"); code. -- Mimano G. Muthondu, Software Developer skype : gmimano Mobile : +254 723 615 206

"process ID" implies a platform-specific thing. Do all platforms have "process IDs"? Maybe, maybe not. And even if they do, are the IDs always of integer type? Maybe, maybe not. Anyway, being a platform-specific thing, and knowing that java is designed for platform-independence, the answer must be "no" (not in pure Java, anyway). Have a look at the following URL : http://blog.igorminar.com/2007/03/how-java-application-can-discover-its.html To send a CTRL+C signal, you may use the ShutdownHook from the sun packages, however I wouldn't recommend it in a production application. Use caution when using the classes from sun package because it contains undocumented support classes that may change between releases of Java. The URL's below should be of some help. http://www.esus.com/docs/GetQuestionPage.jsp?uid=392 http://www.ibm.com/developerworks/java/library/i-signalhandling/ https://lists.xcf.berkeley.edu/lists/advanced-java/2000-August/011724.html http://forums.sun.com/thread.jspa?threadID=5270353 Hope this helps.

Thanks. I had looked at these links. Perhaps I still hoped there was a more generic way of doing it. :-(. Arrrrrrrrrrgh! On 6 July 2010 07:03, Frankline <fraogongi@gmail.com> wrote:
"process ID" implies a platform-specific thing. Do all platforms have "process IDs"? Maybe, maybe not. And even if they do, are the IDs always of integer type? Maybe, maybe not. Anyway, being a platform-specific thing, and knowing that java is designed for platform-independence, the answer must be "no" (not in pure Java, anyway). Have a look at the following URL :
http://blog.igorminar.com/2007/03/how-java-application-can-discover-its.html
To send a CTRL+C signal, you may use the ShutdownHook from the sun packages, however I wouldn't recommend it in a production application. Use caution when using the classes from sun package because it contains undocumented support classes that may change between releases of Java. The URL's below should be of some help.
http://www.esus.com/docs/GetQuestionPage.jsp?uid=392 http://www.ibm.com/developerworks/java/library/i-signalhandling/ https://lists.xcf.berkeley.edu/lists/advanced-java/2000-August/011724.html http://forums.sun.com/thread.jspa?threadID=5270353
Hope this helps.
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------ Skunkworks Server donations spreadsheet
http://spreadsheets.google.com/ccc?key=0AopdHkqSqKL-dHlQVTMxU1VBdU1BSWJxdy1f... ------------ Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
-- Mimano G. Muthondu, Software Developer skype : gmimano Mobile : +254 723 615 206

On Tue, Jul 6, 2010 at 12:51 AM, Geoffrey Mimano <soyfactor@gmail.com> wrote:
gksu rfcomm connect 0 0B:D4:91:39:66:01
They are not matching because the jvm invokes its own shell starts up rfcomm and returns and I am guessing this rfcomm command runs 'headless' in the main os shell. so what you need to do is get the process id of the running rfcomm : the output of this shell command should give you the real process id of the rfcomm process . "ps ax | grep 'rfcomm' | awk '{print $1}' " which you can then you to send a kill signal...

Ashok, Thanks for the info. I had tried that but what I got was a list of processes like so. 1272 5785 5791 Problem is that I need to associated the pid to individual process (gotten from Runtime.getRuntime().exec("gksu rfcomm connect 0 0B:D4:91:39:66:01");) in the java app. Now since I am using Threading ie. the Runtime.getRuntime().exec(" gksu rfcomm connect 0 0B:D4:91:39:66:01"); is being called within a thread many times for the different array of devices that I have set on my machine I have to make sure I do not kill a process that has not completed its task. Its not possible to determine the execution order of threads so I need the calling thread to retrieve its own pid assicaited with invoked runtime process. I think I might just use some shell scripts and call them from within java then try retrieve the shell scripts from with the java code. (Messy stuff :-( ) On 6 July 2010 11:06, Ashok Hariharan <ashok@parliaments.info> wrote:
On Tue, Jul 6, 2010 at 12:51 AM, Geoffrey Mimano <soyfactor@gmail.com> wrote:
gksu rfcomm connect 0 0B:D4:91:39:66:01
They are not matching because the jvm invokes its own shell starts up rfcomm and returns and I am guessing this rfcomm command runs 'headless' in the main os shell.
so what you need to do is get the process id of the running rfcomm :
the output of this shell command should give you the real process id of the rfcomm process .
"ps ax | grep 'rfcomm' | awk '{print $1}' "
which you can then you to send a kill signal... _______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------ Skunkworks Server donations spreadsheet
http://spreadsheets.google.com/ccc?key=0AopdHkqSqKL-dHlQVTMxU1VBdU1BSWJxdy1f... ------------ Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
-- Mimano G. Muthondu, Software Developer skype : gmimano Mobile : +254 723 615 206

On Tue, Jul 6, 2010 at 1:32 PM, Geoffrey Mimano <soyfactor@gmail.com> wrote:
Problem is that I need to associated the pid to individual process (gotten from Runtime.getRuntime().exec("gksu rfcomm connect 0 0B:D4:91:39:66:01");) in the java app. Now since I am using Threading ie. the Runtime.getRuntime().exec(" gksu rfcomm connect 0 0B:D4:91:39:66:01"); is being called within a thread many times for the different array of devices that I have set on my machine I have to make sure I do not kill a process that has not completed its task.
what you need to do is run rfcomm from within a shell script and invoke the shell script from java : gksu rfcomm connect 0 0B:D4:91:39:66:01 & lastpid=$! echo "${LASTPID} $! - returns the pid of the last forked process which in this case is the rfcomm process ... to check if its still running you can use pgrep or pipe-filter ps -ax ...

SWEET! On 6 July 2010 14:16, <ashok+skunkworks@parliaments.info<ashok%2Bskunkworks@parliaments.info>
wrote:
On Tue, Jul 6, 2010 at 1:32 PM, Geoffrey Mimano <soyfactor@gmail.com> wrote:
Problem is that I need to associated the pid to individual process (gotten from Runtime.getRuntime().exec("gksu rfcomm connect 0 0B:D4:91:39:66:01");) in the java app. Now since I am using Threading ie. the Runtime.getRuntime().exec(" gksu rfcomm connect 0 0B:D4:91:39:66:01"); is being called within a thread many times for the different array of devices that I have set on my machine I have to make sure I do not kill a process that has not completed its task.
what you need to do is run rfcomm from within a shell script and invoke the shell script from java :
gksu rfcomm connect 0 0B:D4:91:39:66:01 & lastpid=$! echo "${LASTPID}
$! - returns the pid of the last forked process which in this case is the rfcomm process ...
to check if its still running you can use pgrep or pipe-filter ps -ax ... _______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------ Skunkworks Server donations spreadsheet
http://spreadsheets.google.com/ccc?key=0AopdHkqSqKL-dHlQVTMxU1VBdU1BSWJxdy1f... ------------ Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
-- Mimano G. Muthondu, Software Developer skype : gmimano Mobile : +254 723 615 206

Have a look at these: http://www.jroller.com/santhosh/entry/get_current_java_process_id http://www.scheibli.com/projects/getpids/index.html On Tue, Jul 6, 2010 at 2:16 PM, <ashok+skunkworks@parliaments.info> wrote:
On Tue, Jul 6, 2010 at 1:32 PM, Geoffrey Mimano <soyfactor@gmail.com> wrote:
Problem is that I need to associated the pid to individual process (gotten from Runtime.getRuntime().exec("gksu rfcomm connect 0 0B:D4:91:39:66:01");) in the java app. Now since I am using Threading ie. the Runtime.getRuntime().exec(" gksu rfcomm connect 0 0B:D4:91:39:66:01"); is being called within a thread many times for the different array of devices that I have set on my machine I have to make sure I do not kill a process that has not completed its task.
what you need to do is run rfcomm from within a shell script and invoke the shell script from java :
gksu rfcomm connect 0 0B:D4:91:39:66:01 & lastpid=$! echo "${LASTPID}
$! - returns the pid of the last forked process which in this case is the rfcomm process ...
to check if its still running you can use pgrep or pipe-filter ps -ax ... _______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------ Skunkworks Server donations spreadsheet http://spreadsheets.google.com/ccc?key=0AopdHkqSqKL-dHlQVTMxU1VBdU1BSWJxdy1f... ------------ Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
-- Regards, Brian Ngure

this worked for me sometime back: String pid = ManagementFactory.getRuntimeMXBean().getName(); String[] pIdArray = pid.split("@"); //arraying Long myPid = Long.valueOf(pIdArray[0]); //value returned of curr process -- regards, S.N.Maina ExtremeGrafixDesign Ltd. www.creativepitome.com steve@creativepitome.com P.O BOX 193 Ngong Hills +254726489473,+254771461638 "I played a blank tape at full volume. The mime who lived next door complained, so I shot him with a gun with a silencer." -- Stephen Wright
participants (6)
-
Ashok Hariharan
-
ashok+skunkworks@parliaments.info
-
Brian Ngure
-
Frankline
-
Geoffrey Mimano
-
NDUNGU STEPHEN MAINA P15/2727/2007