Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you just want to run some shutdown specific code, the "proper Java" way to handle this would not use signals, but would instead add a generic "shutdown hook" that would run when your application was about to terminate. This is one of those least-common-denominator problems that Java sometimes suffers from. (Since not all platforms support <code>SIGINT</code>, in Java, no platform supports <code>SIGINT</code>.)</p> <p>Unfortunately, you don't get much context in a <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Runtime.html#addShutdownHook%28java.lang.Thread%29"><code>ShutdownHook</code></a>, but it may still be useful:</p> <pre><code>Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { public void run() { // cleanup } })); </code></pre> <p>If you really need to distinguish between the signal received, or you want to support signals that Java normally ignores (like <code>USR1</code>), or you want to abort shutdown based on a signal, then a <code>ShutdownHook</code> will be mostly useless for you, unfortunately.</p> <p>There is a non-supported, poorly-documented way to capture signals in the Sun JVM, using the <code>sun.misc.SignalHandler</code> class. This is questionably portable, though it appears that the <a href="http://www.ibm.com/developerworks/ibm/library/i-signalhandling/">IBM JVM also supports this</a>.</p> <p>For example - you could hook up a signal handler to listen to <code>SIGHUP</code> reload your server configuration, which was set up in the <code>init.d</code> script as the <code>reload</code> verb:</p> <pre><code>Signal.handle(new Signal("HUP"), new SignalHandler() { public void handle(Signal signal) { reloadConfiguration(); } }); </code></pre> <p>As for configuring a Java application to be controlled using the <code>system</code> command, you should write a shell script in <code>init.d</code> program that starts it up. This simply needs to respond to the <code>start</code> and <code>stop</code> verbs and take the appopriate action. For example, this could be your <code>/etc/init.d/my-java-program</code>:</p> <pre><code>#!/bin/sh case "$1" in start) java /path/to/my/java/program.jar &amp; echo $! &gt; /var/run/my-java-program.pid ;; stop) if [ ! -f /var/run/my-java-program.pid ]; then echo "my-java-program: not running" exit 1 fi kill -TERM `cat /var/run/my-java-program.pid` ;; reload) if [ ! -f /var/run/my-java-program.pid ]; then echo "my-java-program: not running" exit 1 fi kill -HUP `cat /var/run/my-java-program.pid` ;; *) echo "Usage: /etc/init.d/my-java-program {start|stop|reload}" exit 1 ;; esac exit 0 </code></pre> <p>You can now start your application by running <code>/etc/init.d/my-java-program start</code>, or on CentOS, you can also use service my-java-program start`.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload