This discussion is archived
3 Replies Latest reply: May 6, 2013 1:27 PM by jschellSomeoneStoleMyAlias RSS

Questions regarding signals and Runtime.getRuntime.exec()

1000127 Newbie
Currently Being Moderated
I have a program that wraps another by using the Runtime.getRuntime.exec() method to launch the other process. The program that I am wrapping does not shut down cleanly when it receives a sigterm ie ctrl-c from the terminal. In my program I register a shutdown hook to catch this signal. The intention is to write a stop command to the launched process's standard in and block execution until the process terminates. When I hit ctrl-c from the terminal line my shutdown hook gets called but sigterm is issued to the process that I launched also so it shuts down dirty before I get a chance to write the proper shutdown command to its standard in. My question is who is sending sigterm to the launched process. Is it the JVM or is it the terminal emulator that I'm running in Ubuntu.
  • 1. Re: Questions regarding signals and Runtime.getRuntime.exec()
    910542 Newbie
    Currently Being Moderated
    When you use Runtime.getRuntime.exec() the command is sent from you terminal and not from the JVM.
    You can try to apply a stop command which you catch in OS space and close the exec correctly, so the application starting it will know about it.
  • 2. Re: Questions regarding signals and Runtime.getRuntime.exec()
    EJP Guru
    Currently Being Moderated
    When you use Runtime.getRuntime.exec() the command is sent from you terminal and not from the JVM.
    Complete nonsense. The JVM calls one of the varieties of exec(2), or WinExec() on Windows. What you describe neither exists nor even makes sense.
    You can try to apply a stop command which you catch in OS space and close the exec correctly, so the application starting it will know about it.
    More nonsense.

    Where do you get this stuff?
  • 3. Re: Questions regarding signals and Runtime.getRuntime.exec()
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    997124 wrote:
    ...before I get a chance to write the proper shutdown command to its standard in.
    Presumably all of that means that
    - you are using Runtime.addShutdownHook()
    - You have explicitly determine that your shutdown hook does not complete. That means that you used a mechanism such as logging to determine that you method was running.
    - Your method is not throwing exception - you have catch(Throwable) and log that.

    If all of the above is true and only if it is true then your only alternative would be to put a keep alive in place. That would be a communcation from the calling app to the child app such that the child app terminates itself if the calling app doesn't send it. That is the only safe way since there are cases where shutdown hooks would not be called. You can also write a clean up app that looks for orphaned child processes and kills them.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points