Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
471 views
in Technique[技术] by (71.8m points)

exit - Java: How to handle a SIGTERM only?

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 to override only the sigterm shutdown handler but retain the handlers for the rest of the signals.

details of signals
a slight variation of this qns

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...