Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The way to handle this for anything <strong>other</strong> than <code>kill -9</code> would be to register a <a href="http://java.sun.com/javase/6/docs/api/java/lang/Runtime.html#addShutdownHook%28java.lang.Thread%29" rel="noreferrer">shutdown</a> hook. If you can use (<strong>SIGTERM</strong>) <code>kill -15</code> the shutdown hook will work. (<strong>SIGINT</strong>) <code>kill -2</code> <strong>DOES</strong> cause the program to gracefully exit and run the shutdown hooks.</p> <blockquote> <p>Registers a new virtual-machine shutdown hook.</p> <p>The Java virtual machine shuts down in response to two kinds of events:</p> <pre><code>* The program exits normally, when the last non-daemon thread exits or </code></pre> <p>when the exit (equivalently, System.exit) method is invoked, or</p> <pre><code>* The virtual machine is terminated in response to a user </code></pre> <p>interrupt, such as typing ^C, or a system-wide event, such as user logoff or system shutdown.</p> </blockquote> <p>I tried the following test program on OSX 10.6.3 and on <code>kill -9</code> it did <strong>NOT</strong> run the shutdown hook, didn't think it would. On a <code>kill -15</code> it <strong>DOES</strong> run the shutdown hook every time.</p> <pre><code>public class TestShutdownHook { public static void main(final String[] args) throws InterruptedException { Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { System.out.println("Shutdown hook ran!"); } }); while (true) { Thread.sleep(1000); } } } </code></pre> <p>There isn't any way to really gracefully handle a <code>kill -9</code> in any program.</p> <blockquote> <p>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.</p> </blockquote> <p>The only real option to handle a <code>kill -9</code> is to have another watcher program watch for your main program to go away or use a wrapper script. You could do with this with a shell script that polled the <code>ps</code> command looking for your program in the list and act accordingly when it disappeared.</p> <pre><code>#!/usr/bin/env bash java TestShutdownHook wait # notify your other app that you quit echo "TestShutdownHook quit" </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload