5

I am writing a program where i am creating multiple threads in a process.

I need to handle that if the process is killed externally by someone by using kill -9 signal or Ctrl + C, my program should do some action before closing e.g. it should change the status of process to aborted in database.

How can i handle that ?

Do i need addShutdownHook() ? or is there any other better solution to my problem ?

I have added :

Runtime.getRuntime().addShutdownHook( new Thread() {

            @Override
            public void run() {
                logger.info( "Running Shutdown Hook" );
                //call some method
                System.out.println( "Running Shutdown Hook" );
            }
        } );

inside my main method, But it doesn't seem to work.

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
chumak
  • 117
  • 1
  • 9

2 Answers2

2

Short answer: probably won't work.

See JavaDoc for that addShutdownHook():

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... If the virtual machine aborts then no guarantee can be made about whether or not any shutdown hooks will be run.

In other words: a shutdown hook is not a robust choice to address your requirements.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • 1
    Shutdown hook can be used for Kill -15 and even Ctrl-C. Make a note not to use kill -9 recklessly and should work as expected as long as it's properly used. – caburse Dec 23 '16 at 14:56
0

Did you put it in a place where it would be instantiated? Try running the following and killing it

 import java.util.concurrent.TimeUnit;

 public class KillTest {

    public static void main(String args[]) {
       Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                call some method
                System.out.println("Running Shutdown Hook");
            }
        });

        try{
            TimeUnit.MINUTES.sleep(10);
        }catch(Exception e){
            System.out.println("Error thrown");
        }finally {
            System.out.println("How awesome is finally?");
        }
    }
}
caburse
  • 156
  • 6
  • of course you need to add a logger if it's running in the background – caburse Dec 23 '16 at 14:33
  • 2
    I kinda dont get your answer. What is the point of **sleeping** when the process received a SHUTDOWN signal? Besides ... I think my answer is well indicating that the shutdown hook does not work for "kill" signal. Thus it doesn't help at all to add some try/catch with a sleep to the original code. – GhostCat Dec 23 '16 at 14:37
  • @caburse yes i have tried this, but it doesn't print anything :( – chumak Dec 23 '16 at 14:39
  • @GhostCat if addShutdownHook() doesn't work for my requirements. Then what are the possible options to try to handle kill signals ? – chumak Dec 23 '16 at 14:40
  • My point was to make sure the shutdown hook was actually instantiated. That's why i used your example and put it at the beginning of the call. Sleeping is an easy way to keep the program running while i find the process and kill it. – caburse Dec 23 '16 at 14:43
  • @caburse Yeah, running windows. Questioner is asking about "kill -9" which means some sort of Linux system probably. – GhostCat Dec 23 '16 at 14:45
  • 1
    @chumak The fact that I know what doesn't work ... does not imply that I know what would work. If I would know, I would have told you already ;-( ... I guess you will have to some more research. Worst case, the whole approach doesn't fly; and you have to find a way to *change* your requirements somehow. – GhostCat Dec 23 '16 at 14:46
  • 1
    Just reread your requirements. It works with Ctrl C or a clean shut down kill -15 but not kill -9. – caburse Dec 23 '16 at 14:50
  • Better conversations here. http://stackoverflow.com/questions/2541597/how-to-gracefully-handle-the-sigkill-signal-in-java – caburse Dec 23 '16 at 14:54
  • For the record:when you read my answer carefully... I am saying that this solution is probably not robust! The javadoc is clear about rare aborts... Thus:the fact that this code works for you is **not** a guarantee that it would work all the time for the questioner! – GhostCat Dec 23 '16 at 15:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/131367/discussion-between-caburse-and-ghostcat). – caburse Dec 23 '16 at 16:08