Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Signal handling using "TERM"

Tags:

java

signals

I have a standalone application in which I have to prompt the user with an confirm dialog box to save the changes made by him when he tries to shutdown the system by start-->shutdown.

I came to know that by using signalhandlers we can do it.
Can some one help me how to use signal handlers

like image 239
chaithu Avatar asked Sep 11 '09 05:09

chaithu


People also ask

How are signals handled?

A signal is a software interrupt delivered to a process. The operating system uses signals to report exceptional situations to an executing program. Some signals report errors such as references to invalid memory addresses; others report asynchronous events, such as disconnection of a phone line.

What is term signal?

1 : sign, indication. 2a : an act, event, or watchword that has been agreed on as the occasion of concerted action. b : something that incites to action. 3 : something (such as a sound, gesture, or object) that conveys notice or warning.

What is signal handling in network programming?

A signal is a notification to a process that an event has occurred. Signals are sometimes called software interrupts. Signals usually occur asynchronously. By this we mean that a process doesn't know ahead of time exactly when a signal will occur.


1 Answers

Update May 2012 (2 and half years later)

Trejkaz comments:

On current versions of Java this signal handling code fails because the "INT" signal is "reserved by the VM or the OS".
Additionally, none of the other valid signal names actually fire when something requests the application to close (I just painstakingly tested all of the ones I could find out about...)
The shutdown hook mostly works but we find that in our case it isn't firing, so the next step is obviously to resort to registering a handler behind the JVM's back

The chapter "Integrating Signal and Exception Handling" of the "Troubleshooting Guide for HotSpot VM" mentions the signals "SIGTERM, SIGINT, SIGHUP" only for Solaris OS and Linux.
Only Exception Handling on Windows are mentioned.


Original answer (Sept 2009)

a ShutdownHook should be able to handle that case

Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
    public void run() {
        // what you want to do
    }
}));

(with caveats)
See also:

  • Java signal handling and termination (link is dead, see archive or mirror)
  • java exit signal handling:

as an illustration of simple signal handling:

public class Aaarggh {
  public static void main(String[] args) throws Exception {
    Signal.handle(new Signal("INT"), new SignalHandler () {
      public void handle(Signal sig) {
        System.out.println(
          "Aaarggh, a user is trying to interrupt me!!");
        System.out.println(
          "(throw garlic at user, say `shoo, go away')");
      }
    });
    for(int i=0; i<100; i++) {
      Thread.sleep(1000);
      System.out.print('.');
    }
  }
}
like image 55
VonC Avatar answered Oct 07 '22 00:10

VonC