How can I get the PID of the process started by Runtime.exec() on *nix - plattforms to kill it afterwards ?
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Alex_Odesser
Posted On:   Monday, August 5, 2002 03:05 AM

I would like to give my customers the possibility to kill the native processes started by my java - server - application using Runtime.exec() - function. Normally I do it f.e. to start the sleep - command with ( very simplified ) String cmd[] = {"sh", "-c", "sleep 60"}; Process p = Runtime.exec(cmd); p.waitFor(); int rc = p.exitValue(); I would like to kill the Process sleep with another thread with p.destroy() or something like this but I if I do it so, I will kill only the shell process. I need to get the unix - pid of shell process to find out the pid of sleep on the native way. Have you an idea ?    More>>

I would like to give my customers the possibility to kill the native processes started by my java - server - application using Runtime.exec() - function. Normally I do it f.e. to start the sleep - command with ( very simplified )

String cmd[] = {"sh", "-c", "sleep 60"};

Process p = Runtime.exec(cmd);

p.waitFor();

int rc = p.exitValue();






I would like to kill the Process sleep with another thread with p.destroy() or something like this but I if I do it so, I will kill only the shell process. I need to get the unix - pid of shell process to find out the pid of sleep on the native way.



Have you an idea ?



regards

Alex

   <<Less

Re: How can I get the PID of the process started by Runtime.exec() on *nix - plattforms to kill it afterwards ?

Posted By:   Henrik_Gram  
Posted On:   Wednesday, October 9, 2002 03:25 AM

Had a similar problem and I ended up doing:

sh -c 'echo $$ && exec sleep 234567'

This will print the pid of the shell that was started and since exec replaces the program image with the new one, the sleep process gets the same pid. You can use this with the Process object and then read the first line from it which will then contain the PID.

Cheers,

Henrik

Re: How can I get the PID of the process started by Runtime.exec() on *nix - plattforms to kill it afterwards ?

Posted By:   Christopher_Koenigsberg  
Posted On:   Monday, August 5, 2002 06:54 AM

Man "ps".

About | Sitemap | Contact