简体   繁体   中英

Java application does not receive SIGNALs on Ubuntu 12.04

Strange problem

I'm trying to shutdown my Java app properly on receiving signal, either send manually via kill. I tried kill SIGTERM, SIGHUP, SIGINT etc. and every time JVM just stops without calling Runtime shutdown hooks, finally block or signal traps created in Java code.

    Runtime.getRuntime().addShutdownHook(new Thread("Shutdown hook") {
        @Override
        public void run() {
            vd.pretendRemoveAll();
        }
    });

And added handlers when shutdownHook did not work

    SignalHandler h = new SignalHandler() {
        @Override
        public synchronized void handle(Signal sig) {
            _logger.warn("Received signal: {}", sig);
            vd.pretendRemoveAll();
            System.exit(0);
        }
    };
    Signal.handle(new Signal("INT"), h);
    Signal.handle(new Signal("KILL"), h);
    Signal.handle(new Signal("HUP"), h);
    Signal.handle(new Signal("TERM"), h);

I'm running on Ubuntu 12.04 with Java java version "1.6.0_24"

OpenJDK Runtime Environment (IcedTea6 1.11.5) (6b24-1.11.5-0ubuntu1~12.04.1)

OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)

I tried also with Oracle Java jdk1.6.0_37, the same effect

When I use strace on the process to see what happens I get:

user@server:~/appdir$ sudo strace -v -p 32277Process 32277 attached - interrupt to quitfutex(0x7f667f07d9d0, FUTEX_WAIT, 32278, NULL) = ? ERESTARTSYS (To be restarted) --- SIGTERM (Terminated) @ 0 (0) --- futex(0x7f667e2543e0, FUTEX_WAKE_PRIVATE, 1) = 1 rt_sigreturn(0x7f667e2543e0) = 202 futex(0x7f667f07d9d0, FUTEX_WAIT, 32278, NULLPANIC: attached pid 32277 exited with 143

And this NULLPANIC looks suspicious, but I have no idea what next.

I tested that code on Mac and works no problem. Any ideas what's causing the problem and how to fix that? Is it some security feature/policy that needs to be set?

This code bellow inspired from http://hellotojavaworld.blogspot.fr/2010/11/runtimeaddshutdownhook.html , runs well on Ubuntu, jdk 1.6.30, how do you call the addShutdownHook method ? you must be in a Class instrance and not in the static main method.

public class AddShutdownHookSample {
 public void attachShutDownHook(){
  Runtime.getRuntime().addShutdownHook(new Thread() {
   @Override
   public void run() {
    System.out.println("Inside Add Shutdown Hook");
   }
  });
  System.out.println("Shut Down Hook Attached.");
 }
public static void main(String[] args) throws InterruptedException {

  AddShutdownHookSample sample = new AddShutdownHookSample();
  sample.attachShutDownHook();
        Thread.sleep(1000*60*1);
            //Print a message
  System.out.println("Last instruction of Program....");
  System.exit(0);
 }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM