Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I catch moment when someone kill java.exe process?

Tags:

java

console java application. Someone kill java.exe process by Task Manager. How can I write to logs some information at this moment before application is terminated?

Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
    public void uncaughtException(Thread t, Throwable e) { ..... }
}); 

OR

Runtime.getRuntime().addShutdownHook(new Thread() {
    public void run() { ..... }
});

don't execute in such situation

like image 570
Mikhail Avatar asked Nov 17 '25 20:11

Mikhail


1 Answers

This is not possible. Shutdown hooks will only be executed in an orderly shutdown:

In rare circumstances the virtual machine may abort, that is, stop running without shutting down cleanly. This occurs when the virtual machine is terminated externally, for example with the SIGKILL signal on Unix or the TerminateProcess call on Microsoft Windows.

You can send another signal that will trigger an orderly shutdown like SIGINT. Killing a application should be the last resort after the application did not respond.

like image 174
Thomas Jung Avatar answered Nov 20 '25 10:11

Thomas Jung



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!