Handle if a process killed externally
Asked Answered
B

2

6

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.

Bolitho answered 23/12, 2016 at 14:18 Comment(0)
P
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.

Pibgorn answered 23/12, 2016 at 14:24 Comment(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.Hamamatsu
H
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?");
        }
    }
}
Hamamatsu answered 23/12, 2016 at 14:33 Comment(11)
of course you need to add a logger if it's running in the backgroundHamamatsu
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.Pibgorn
@Hamamatsu yes i have tried this, but it doesn't print anything :(Bolitho
@Pibgorn if addShutdownHook() doesn't work for my requirements. Then what are the possible options to try to handle kill signals ?Bolitho
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.Hamamatsu
@Hamamatsu Yeah, running windows. Questioner is asking about "kill -9" which means some sort of Linux system probably.Pibgorn
@Bolitho 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.Pibgorn
Just reread your requirements. It works with Ctrl C or a clean shut down kill -15 but not kill -9.Hamamatsu
Better conversations here. #2542097Hamamatsu
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!Pibgorn
Let us continue this discussion in chat.Hamamatsu

© 2022 - 2024 — McMap. All rights reserved.