Shutting Down with UNIX

I am trying to get my Java program to exit gracefully on my unix server. I have a jar file that I start with cron in the morning. Then in the evening, when I want to close it, I have a cron job that calls a script that finds the PID and calls kill -9 <PID> . However, it looks like my switch is off when I finish this path. I also tried kill <PID> (no -9) and I get the same problem. How can I make sure that the calling call is being called? Alternatively, there may be a better way to kill my process daily.

 class ShutdownHook { ShutdownHook() {} public void attachShutDownHook() { Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { System.out.println("Shut down hook activating"); } }); System.out.println("Shut Down Hook Attached."); } } 
+4
source share
3 answers

You can use code like this on Unix to capture a SIGINT signal (# 2):

 Signal.handle(new Signal("INT"), new SignalHandler() { public void handle(Signal sig) { // Forced exit System.exit(1); } }); 
+6
source

kill -9 <pid> sends a KILL signal. This signal cannot be intercepted by the program.

If you call kill <pid> , a TERM (15) signal will be sent. In this case, the JVM will catch the signal and stop hooks will be executed.

+4
source

This has nothing to do with the signals that the JVM captures / receives, but all that has to do with the terrible termination of the Gnome process, which apparently should be incompatible with not completely pinching the bed (and jdk does not have an api for this ) If you want to see an even worse consequence of this, try running:

 dbus-monitor --profile --session type='method_call',interface='org.gnome.SessionManager' 

in the shell and logout or restart: it will cause the gnome shell to crash and hang up the computer until you enter TTY and order a restart. Maybe kdbus will fix it in this case, maybe not. The only thing I know is that shutdownhooks on a Java application using AWT (not on the command line) NEVER launch their shutdownhooks on GNOME3. In fact, a virtual machine will always exit with a non-zero code (failure), presumably from native code. At least it does not hang, although this makes the disconnect hooks completely useless (I tried to make a workaround using the dbus monitor, but, as you can see from the example I gave, this is too dangerous).

0
source

Source: https://habr.com/ru/post/1442144/


All Articles