Make sure that the program launched through Runtime.exec () will die with the java application

in my java program. I call an external program through Runtime.execand call Process.waitForto wait for its completion. This is a somewhat lengthy script. I want to make sure that if something goes wrong with my java application (for example, it will be killed from the outside, for example, which sometimes happens sometimes in my case), the external script run will also die.

I looked at Runtime.addShutdownHookwhich may be suitable for this, but it clearly states that, for example, SIGKILLcannot be guaranteed whether the shutdownhook will work or not.

Is there any other way to ensure that an external program launched from Java will die with the calling Java process?

thank

+3
source share
1 answer

If "Runtime.addShutdownHook" is not enough, I think you are out of luck on the java side. Some ideas:

  • your script can check if the java application is running and terminate it if necessary
  • let your java application update the timestamp file and let your script periodically check if the timestamp is old (and terminate)
  • Edit: start another process that only controls the java and script application and kills the script because the java application is gone (of course, if this process is killed, you are not lucky again)
+1
source

All Articles