0

I have a parent JVM (which I'll call JVM_parent) and inside it I launched another JVM using Runtime.getRuntime().exec("java .......") (which I'll call JVM_child)

Now when I shutdown (either gracefully or forcefully) JVM_parent, the JVM_child also exits.

How can I prevent the JVM_child from being shut down when the JVM_parent is shut down? I'm running on Linux.

AlBlue
  • 23,254
  • 14
  • 71
  • 91
Shubham Chaurasia
  • 2,472
  • 2
  • 15
  • 22
  • 1
    You can try this answer: http://stackoverflow.com/questions/931536/how-do-i-launch-a-completely-independent-process-from-a-java-program The issue is that you need to detach the child process from the parent and that procedure differs per operating system. – sagneta Apr 08 '16 at 11:55
  • @sagneta +1 well yes detaching parent and child is the key. let me try. – Shubham Chaurasia Apr 08 '16 at 12:00
  • not able to detach using bash -c or /bin/sh -c options. – Shubham Chaurasia Apr 08 '16 at 13:48
  • Why not? Every Linux system has the `bash` command! – Stephen C Apr 08 '16 at 13:56
  • 1
    The only other thing I can think of is to create a bash script that executes your other process with a 'nohup &'. That is you want a line like: nohup ./myprogram & – sagneta Apr 08 '16 at 13:56
  • you might want to look into jnr-posix, it provides better access to libc APIs than the standard ProcessBuilder. – the8472 Apr 08 '16 at 14:18
  • update: When I kill parent by CTRL+C, JVM_child is also killed. But when I kill JVM_parent by kill -9, JVM_child survives. I know that CTRL+C raises a SIGINT and kill -9 raises a SIGKILL but anyhow SIGKILL is more brutal than SIGINT – Shubham Chaurasia Apr 11 '16 at 09:17
  • Any reasons why is this happening? – Shubham Chaurasia Apr 11 '16 at 12:53

1 Answers1

0

The child process will inherit from the parent such that kill signals will also kill the child process. To prevent this you need to detach the child from the parent so it won't receive those signals and will continue on.

The easiest way to use this is running something like bash nohup prog & which runs it in a separate child and doesn't listen to the HUP signal. Note that running bash -c is not sufficient. You can tell using something like pstree whether the bash process has finished.

AlBlue
  • 23,254
  • 14
  • 71
  • 91