Results 1 to 9 of 9

Thread: Java Shutdown Hook and SIGTERM

  1. #1
    Join Date
    Oct 2007
    Beans
    4

    Question Java Shutdown Hook and SIGTERM

    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.
    Last edited by HuseUrDaddy; October 11th, 2008 at 09:57 PM. Reason: typo(s)

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Beans
    2,185
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Java Shutdown Hook and SIGTERM

    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.
    Ubuntu 12.04

  3. #3
    Join Date
    Jul 2008
    Beans
    1,491

    Re: Java Shutdown Hook and SIGTERM

    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.

  4. #4
    Join Date
    Oct 2007
    Beans
    4

    Re: Java Shutdown Hook and SIGTERM

    Quote Originally Posted by Reiger View Post
    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.

  5. #5
    Join Date
    Oct 2008
    Location
    White House
    Beans
    361

    Re: Java Shutdown Hook and SIGTERM

    There's a powerful and crossplatform solution
    Code:
    javax.swing.JOptionPane.showMessageDialog( null, "Attention", "Please press your computer's shutdown button", 1 );

  6. #6
    Join Date
    Jul 2008
    Beans
    1,491

    Re: Java Shutdown Hook and SIGTERM

    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...

  7. #7
    Join Date
    Oct 2008
    Location
    White House
    Beans
    361

    Re: Java Shutdown Hook and SIGTERM

    That was sarcasm

  8. #8
    Join Date
    Jul 2008
    Beans
    1,491

    Re: Java Shutdown Hook and SIGTERM

    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.

  9. #9
    Join Date
    May 2007
    Beans
    3

    Re: Java Shutdown Hook and SIGTERM

    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)
    Last edited by i30817; July 12th, 2012 at 05:37 PM.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •