Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux Shutdown and Java Shutdown Hook

When I run a Java process in background and I shut down the computer (ArchLinux), will the computer wait some seconds for the termination of my shutdown-hook in Java?

like image 724
Harald Avatar asked Mar 26 '26 20:03

Harald


1 Answers

In case you want to do something on system shutdown, you need to add a shutdown-hook (assuming you already do this) :

Runtime.getRuntime().addShutdownHook(
new Thread(new Runnable() {
    @Override
    public void run() {
        // This method will be executed and Virtual Machine will close when this method will return
    }
}));

The system will NEVER await a full termination if it took too much time

From the doc Runtime#addShutdownHook

When the virtual machine is terminated due to user logoff or system shutdown the underlying operating system may only allow a fixed amount of time in which to shut down and exit. It is therefore inadvisable to attempt any user interaction or to perform a long-running computation in a shutdown hook.

like image 65
Xephi Avatar answered Mar 29 '26 10:03

Xephi