3

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

Community
  • 1
  • 1
zeroin23
  • 1,821
  • 4
  • 20
  • 26
  • 1
    What parts of the explanations you link to don't you understand? – Brian Roach May 01 '11 at 06:02
  • I am confused - why does the service die when the user logs off? Ideally your service should cater to multiple requests from multiple users, no? – lobster1234 May 01 '11 at 06:45
  • 2
    please post a [SSCCE](http://sscce.org/) of your code, explain the environment where your code is running, and describe the situation in which your code is not behaving as you expect. – Lesmana May 01 '11 at 09:00
  • I am using the http://jslwin.sourceforge.net/ to run my program as a window XP service. It is supposed to continue running even when the user logon or logoff. But when a shutdown is initialed, the service should shutdown also. Currently when the user logoff the service will exit (unwanted behavior). So i would like to just override the handling of Sigterm but retain the rest of the original JVM handlers. – zeroin23 May 03 '11 at 05:52

1 Answers1

3

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.
  }
}
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245