Java: How to handle a SIGTERM only?

后端 未结 1 1113
没有蜡笔的小新
没有蜡笔的小新 2020-12-10 21:58

Is there a way in Java to handle a received SIGTERM?

I am running a java service but do not want to close my java service when the user log off.

Would like

相关标签:
1条回答
  • 2020-12-10 22:28

    If the Signal passed to handle has name "TERM" then do something, otherwise, ignore it.

    class MySignalHandler implements SignalHandler {
      public void handle(Signal sig) {
        if (!"TERM".equals(sig.getName())) {
          SigHandler.SIG_DFL.handle(sig);
          return;
        }
    
        // Handling code goes here.
      }
    }
    
    0 讨论(0)
提交回复
热议问题