Search code examples
javalinuxunixprocesspid

Java Process Termination - onEnd()?


When you force kill a process by PID:

kill -9 1123

Is there any method that is called when the process is terminated, that halts until shutdown?


Solution

  • For java, you can add a ShutdownHook to your process, when sigterm is send to this process, the code in ShutdownHook will be executed before jvm shuts itself down.

        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                //your cleanup codes
            }
        });