I need to be able to start and stop an external program from inside java. I have the starting working just fine but when I stop it, it kills its parent. It turns out that the process I'm starting is killing its entire process group with a kill 0. Does anyone know how to make it so that my java process is not in the process group of the child program?
Asked
Active
Viewed 2,068 times
4
-
@Murali VP: if that's a answer post it as an answer. – user207421 Mar 01 '11 at 23:11
1 Answers
3
So I guess there are two answers:
1) Create your child process as the group leader of a new process group. In Linux I can do this on the command line by using
bash -c "command <args>"
Then you can check that the process group of the new process is different from the terminal you ran the command in with the command:
ps -efj
The 'j' option shows the process group id (PGID).
I will warn you that you may have to escape things weirdly to have it run correctly from Java because the quotes are required for commands with arguments that are passed to the 'bash' command with the '-c' option. So in Java I would guess it would look something like this:
Process processWithNewProcessGroup = Runtime.getRuntime().
exec("bash -c \"sleep 60\"");
2) Change your child process so it doesn't kill everything in its process group.

Kevin S
- 2,713
- 24
- 31