PDA

View Full Version : Java Shutdown Hook and SIGTERM



HuseUrDaddy
October 11th, 2008, 09:54 PM
Hi,

I created a Java Gui Program (using SWT) and Java's Shutdown Hook doesn't work in Ubuntu.

I am using Sun Jre 1.6 installed via the Synaptic Package Manager.

When my application starts, I am creating a lock file and when my application successfully shutsdown, I am deleting the lock file. The delete lock file code is in the shutdown hook thread.

If I leave my application running while I log off or restart Ubuntu, I still see the lock file, meaning, the shutdown hook never got fired.

If I END PROCESS the java process while my application is running (via System Monitor's Processes tab) then the shutdown hook works.

However, if I KILL PROCESS the java process, then the shutdown hook doesn't work.

So, that makes me believe that when Ubuntu shutsdown, restarts or when the user logs off, Ubuntu sends Kill Process (SIGKILL) to the Java process and may be to all processes instead of sending SIGTERM?

One solution I thought was to create a script which will run during Ubuntu's Shutdown and will simply send SIGTERM to my app's Java process.

But its an over-kill. LoL. No puns intended. Any other suggestions will be greatly appreciated.

Thanks.

jespdj
October 11th, 2008, 10:06 PM
This is not a problem that's specific to Ubuntu, the Ubuntu version of Java or Java in general.

The shutdown hook can't be guaranteed to run. When you kill a process (with SIGKILL), the process doesn't get a chance to clean up; the operating system just deletes the process from memory immediately. SIGKILL is meant to stop processes that are in a state where they can't be stopped gracefully.

Read the Java API documentation of the method Runtime.addShutdownHook() carefully. It says:

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. The virtual machine may also abort if a native method goes awry by, for example, corrupting internal data structures or attempting to access nonexistent memory. If the virtual machine aborts then no guarantee can be made about whether or not any shutdown hooks will be run.
So, you should not design your program in such a way that it depends on the shutdown hook to always execute.

Reiger
October 12th, 2008, 01:23 AM
In this case you can compensate for having the clean-up code extracted to a (public static) method of its own.

Now, when you *start* your program you can check for existance of the file indicating a bad shutdown, and clean up the mess before actually starting the program. A bit like a recovery of 'lost' data.

HuseUrDaddy
October 14th, 2008, 12:12 AM
In this case you can compensate for having the clean-up code extracted to a (public static) method of its own.

Now, when you *start* your program you can check for existance of the file indicating a bad shutdown, and clean up the mess before actually starting the program. A bit like a recovery of 'lost' data.

Thanks. My program is already doing that.

Also, I am using HSQLDB for embedded database and it already has a mechanism to detect and recover from abnormal db shutdown.

The Java Shutdown hook works as expected in Windows, Mac and Solaris. Looks like I am out of luck when it comes to Linux. I guess I have to prevent my application from minimizing itself into the system tray otherwise the user might forget to close it when restarting or logging off from Linux.

cl333r
October 14th, 2008, 07:56 AM
There's a powerful and crossplatform solution


javax.swing.JOptionPane.showMessageDialog( null, "Attention", "Please press your computer's shutdown button", 1 );

Reiger
October 14th, 2008, 12:11 PM
I'm sorry, but I don't think people are going to enjoy that one -- especially not as there is no guarantee that it will work on Linux (again) :

requiring people to shutdown next time they start up after a shutdown... ?

At any rate your code would improve if you didn't use a predefined int, but rather one of the JOptionPane constants as it (should) be compatible with any future version of Javax Swing that doesn't deprecate JOptionPanes altogether. :)

What the OP can do, however is looking into ways of sending a signal to his app -- likely through executing scripts on shutdown...

cl333r
October 14th, 2008, 06:49 PM
That was sarcasm

Reiger
October 14th, 2008, 10:27 PM
I'm sorry, my post informs me I should've gone to bed (and sleep) by 3:26. Or at least certainly not engaging in something remotely intelligent. ;)

i30817
July 12th, 2012, 05:33 PM
Bump. This is terrible. How the hell is a program supposed to save state if a kill is sent without a first sending a sigterm? (Which is what i think is happening on restart)