Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way we can execute some java code when a jvm process is killed?

Tags:

java

jvm

We want to execute some java code or shell code before a JVM exits when it is killed manually. Our application is not running in a container. We need to monitor it automatically using the Java code itself or some command line tool.

like image 957
Chris Avatar asked May 06 '14 06:05

Chris


People also ask

How do you kill a Java process gracefully?

Graceful-kill-java allows you to gracefully kill java processes, which 'taskkill pid' and 'wmic ... call terminate' cannot do. TL;DR Use the command graceful-kill-java. bat [command line partial text] to kill all java processes whose command line contains the partial text.

What is the kill command in Java?

Use the command kill -QUIT n to send the signal to a process with process ID (PID) n . Alternatively, press CTRL+\ in the shell window that started Java. The JVM continues after the signal has been handled.

How do you kill JPS process?

ie: kill jps|grep "Server" | cut -d " " -f 1 , there are multiple server processes running and this command does kills all the processes but it takes a while.

How do I run a JVM file?

The way to start a jvm is by invoking the main, either by invoking a jar using java -jar MyJar or by simply running main class from an IDE. Yes, Multiple jvm instances can be run on a single machine, they all will have their own memory allocated. There will be that many jvms as many main programs you run.


2 Answers

You can add a shutdown hook by using Runtime.getRuntime().addShutdownHook(). Such a shutdown hook is run by the JVM, once the JVM' process terminates. However, note that this is not always the case. A JVM might get killed before it gets the chance to trigger its shutdown hooks. This is mentioned in the javadoc:

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.

You can use a shut down hook as follows:

Runtime.getRuntime().addShutdownHook(
  new Thread("app-shutdown-hook") {
    @Override 
    public void run() { 
      System.out.println("bye"); 
    }
});

You can and should also deregister a shut down hook once it is no longer required. Otherwise, the JVM can never garbage collect the hook.

like image 130
bobah Avatar answered Nov 14 '22 23:11

bobah


Not sure what you mean by "killed"?

If it is by a signal, it depends on the signal. For some of these you can add a JVM shutdown hook which the JVM will execute... But not all. SIGKILL is instant death for instance.

With shell code you can do that:

my java command;
# inspect $?

Run that as a background process; you can tell whether the JVM was killed by inspection of $? since it will be 128 or greater (127 + number of signal used to kill).

Note that you can BOTH capture the command output if needed AND inspect $?:

output=$(my java command);
# inspect $? -- ONLY THEN inspect $output

Of course, add quotes where appropriate etc etc.


And of course you can also store the value of $? before inspecting it later:

my command;
RC=$?;
# do something else
# inspect $RC

Finally, it is important to note that in custom code which uses System.exit() you should avoid exiting with 1: the JVM will exit with that return code whenever main() throws anything.

like image 45
fge Avatar answered Nov 14 '22 21:11

fge