Catching Unix signals?

Discussion in 'Java' started by John English, Jun 2, 2004.

  1. John English

    John English Guest

    Is there any way to catch Unix signals inside Java? I've tried a little
    test program that catches Throwable, but using "kill -SIGHUP" or "kill
    -SIGQUIT" just kills the app dead, with no opportunity for me to do any
    cleaning up prior to the application terminating.

    ---------------------------------------------------------------------
    John English | mailto:
    Senior Lecturer | http://www.it.bton.ac.uk/staff/je
    School of Computing Maths & IS | ** NON-PROFIT CD FOR CS STUDENTS **
    University of Brighton | -- see http://burks.bton.ac.uk
    ---------------------------------------------------------------------
     
    John English, Jun 2, 2004
    #1
    1. Advertising

  2. John English wrote:

    Hi,

    > Is there any way to catch Unix signals inside Java? I've tried a little
    > test program that catches Throwable, but using "kill -SIGHUP" or "kill
    > -SIGQUIT" just kills the app dead, with no opportunity for me to do any
    > cleaning up prior to the application terminating.


    http://forum.java.sun.com/thread.jsp?forum=4&thread=514860&tstart=30&trange=15

    Ciao,
    Ronny
     
    Ronny Schuetz, Jun 2, 2004
    #2
    1. Advertising

  3. John English wrote:
    > Is there any way to catch Unix signals inside Java?


    No. But there is Runtime.addShutdownHook()
     
    Michael Borgwardt, Jun 2, 2004
    #3
  4. John English

    iksrazal Guest

    John English <> wrote in message news:<c9kfk7$8k1$>...
    > Is there any way to catch Unix signals inside Java? I've tried a little
    > test program that catches Throwable, but using "kill -SIGHUP" or "kill
    > -SIGQUIT" just kills the app dead, with no opportunity for me to do any
    > cleaning up prior to the application terminating.


    This is from a while back - but I just compiled it with jdk1.4.2 . The
    instances at the bottom do RMI which is otherwise not shown. Of
    course, -9 can't be caught.

    import java.io.*;
    import sun.misc.*;//for signal catching
    import com.protomatter.syslog.*;

    public class MDDBDriver
    {
    public static void main(String[] args)
    {
    SignalHandler handler = new SignalHandler()
    {
    public void handle(Signal sig)
    {
    Syslog.error(this, "SNIFF_SUPPRESSED Explicit kill sent to
    server process, sig is --> "+sig+", aborting...");
    System.exit(0);
    }
    };
    Signal.handle(new Signal("TERM") , handler);//string appened to SIG,
    meaning SIGTERM

    CRMLogRegister instance1= new CRMLogRegister();
    CRMDBPool instance2= new CRMDBPool();
    MDDBRegister instance3= new MDDBRegister();
    }
    }

    HTH

    Outsource to an American programmer living in brazil!
    http://www.braziloutsource.com/
    iksrazal
     
    iksrazal, Jun 2, 2004
    #4
  5. I'm not sure this is always guaranteed to work. As far as I remember
    only the java.* and javax.* classes should be used. The others are for
    internal use only

    Andrea

    iksrazal wrote:
    > John English <> wrote in message news:<c9kfk7$8k1$>...
    >
    >>Is there any way to catch Unix signals inside Java? I've tried a little
    >>test program that catches Throwable, but using "kill -SIGHUP" or "kill
    >>-SIGQUIT" just kills the app dead, with no opportunity for me to do any
    >>cleaning up prior to the application terminating.

    >
    >
    > This is from a while back - but I just compiled it with jdk1.4.2 . The
    > instances at the bottom do RMI which is otherwise not shown. Of
    > course, -9 can't be caught.
    >
    > import java.io.*;
    > import sun.misc.*;//for signal catching
    > import com.protomatter.syslog.*;
    >
    > public class MDDBDriver
    > {
    > public static void main(String[] args)
    > {
    > SignalHandler handler = new SignalHandler()
    > {
    > public void handle(Signal sig)
    > {
    > Syslog.error(this, "SNIFF_SUPPRESSED Explicit kill sent to
    > server process, sig is --> "+sig+", aborting...");
    > System.exit(0);
    > }
    > };
    > Signal.handle(new Signal("TERM") , handler);//string appened to SIG,
    > meaning SIGTERM
    >
    > CRMLogRegister instance1= new CRMLogRegister();
    > CRMDBPool instance2= new CRMDBPool();
    > MDDBRegister instance3= new MDDBRegister();
    > }
    > }
    >
    > HTH
    >
    > Outsource to an American programmer living in brazil!
    > http://www.braziloutsource.com/
    > iksrazal
     
    Andrea Desole, Jun 3, 2004
    #5
  6. John English

    iksrazal Guest

    Andrea Desole <> wrote in message news:<>...
    > I'm not sure this is always guaranteed to work. As far as I remember
    > only the java.* and javax.* classes should be used. The others are for
    > internal use only
    >
    > Andrea
    >


    Its worked for me since jdk 1.2.2 to jdk1.4.2 , and is explained
    nicely from IBM here:

    http://www-106.ibm.com/developerworks/ibm/library/i-signalhandling/

    That being said, I'd like to see a link explaining Sun's position on
    using jdk classes outside of java.* and javax.* , such as sun.misc.* .

    Outsource to an American programmer living in brazil!
    http://www.braziloutsource.com/
    iksrazal
     
    iksrazal, Jun 3, 2004
    #6
  7. Thomas Weidenfeller, Jun 4, 2004
    #7
  8. Well, I have to say that I don't really find Sun very explicit about
    this, but it is possible to find something. Try this page

    http://java.sun.com/products/jdk/faq/faq-sun-packages.html

    Andrea


    iksrazal wrote:
    > Andrea Desole <> wrote in message news:<>...
    >
    >>I'm not sure this is always guaranteed to work. As far as I remember
    >>only the java.* and javax.* classes should be used. The others are for
    >>internal use only
    >>
    >>Andrea
    >>

    >
    >
    > Its worked for me since jdk 1.2.2 to jdk1.4.2 , and is explained
    > nicely from IBM here:
    >
    > http://www-106.ibm.com/developerworks/ibm/library/i-signalhandling/
    >
    > That being said, I'd like to see a link explaining Sun's position on
    > using jdk classes outside of java.* and javax.* , such as sun.misc.* .
    >
    > Outsource to an American programmer living in brazil!
    > http://www.braziloutsource.com/
    > iksrazal
     
    Andrea Desole, Jun 4, 2004
    #8
  9. On Fri, 04 Jun 2004 09:16:05 +0200, Thomas Weidenfeller wrote:

    > All of Sun's SDK documentation contains a link to:


    (shakes head) I missed it Thomas.

    > http://java.sun.com/products/jdk/faq/faq-sun-packages.html


    In fact I remember somebody pointing
    that link out to me (probably you)
    and went *searching for it* at Sun but
    failed to locate it (too many hits for
    'sun package' and could not think how
    to narrow down)..

    If it is there.. it might be it is
    buried amongst hundreds (thousands?)
    of other links in the SDK..

    Or is it one of those 'hidden in small
    print around the edges of the web-page'
    kind of links? ;-)

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
     
    Andrew Thompson, Jun 4, 2004
    #9
  10. Andrew Thompson wrote:
    > (shakes head) I missed it Thomas.

    [...]
    > If it is there.. it might be it is
    > buried amongst hundreds (thousands?)
    > of other links in the SDK..


    It is on the "front page" of the SDK documentations. E.g. on

    http://java.sun.com/j2se/1.5.0/docs/
    http://java.sun.com/j2se/1.4.2/docs/
    http://java.sun.com/j2se/1.3/docs/

    The link is always labeled

    Note About sun.* Packages

    > Or is it one of those 'hidden in small
    > print around the edges of the web-page'
    > kind of links? ;-)


    Nop. normal link.


    /Thomas
     
    Thomas Weidenfeller, Jun 4, 2004
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    12
    Views:
    1,654
    Dave Thompson
    Jan 10, 2005
  2. maxmagna

    UNIX signals & C++

    maxmagna, Mar 4, 2005, in forum: C++
    Replies:
    4
    Views:
    2,310
    maxmagna
    Mar 4, 2005
  3. hg
    Replies:
    1
    Views:
    243
    robert
    Jan 15, 2007
  4. geoffbache
    Replies:
    6
    Views:
    404
    fumanchu
    Jun 11, 2007
  5. Viacheslav Chumushuk

    Catching system signals (UNIX).

    Viacheslav Chumushuk, Jan 15, 2009, in forum: Ruby
    Replies:
    1
    Views:
    115
    Michael Fellinger
    Jan 15, 2009
Loading...

Share This Page