How to gracefully handle the SIGKILL signal in Java

前端 未结 5 2102
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 11:34

How do you handle clean up when the program receives a kill signal?

For instance, there is an application I connect to that wants any third party app (my app) to se

5条回答
  •  时光取名叫无心
    2020-11-22 11:50

    There are ways to handle your own signals in certain JVMs -- see this article about the HotSpot JVM for example.

    By using the Sun internal sun.misc.Signal.handle(Signal, SignalHandler) method call you are also able to register a signal handler, but probably not for signals like INT or TERM as they are used by the JVM.

    To be able to handle any signal you would have to jump out of the JVM and into Operating System territory.

    What I generally do to (for instance) detect abnormal termination is to launch my JVM inside a Perl script, but have the script wait for the JVM using the waitpid system call.

    I am then informed whenever the JVM exits, and why it exited, and can take the necessary action.

提交回复
热议问题