gyurix
gyurix

Reputation: 1086

How can I do some stuff in my java app, after it has been killed?

I would like to make some critical (saving) operations in my java app, which should be executed, when the java process is killed by the task manager or by the IDE. Is it possible to do it anyhow?

EDIT:

Tried with the shutdown hook, but it doesn't work, with process kills, just with normal JVM shutdowns, or exceptions.

What I need is to print the SHUTDOWN word to the console when I kill my program using IntelliJ, but it doesn't work:

public static void main(String[] args) throws InterruptedException {
        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
            public void run() {
                System.out.println("SHUTDOWN");
            }
        }));
        while(true){
            System.out.println("NOTHING");
            Thread.sleep(500);
        }
    }

Upvotes: 1

Views: 398

Answers (1)

Stanton
Stanton

Reputation: 495

Depending on how you choose to 'kill' your java process, this may be of interest to you: How to handle a SIGTERM

Upvotes: 1

Related Questions