Catching Unix signals?

J

John English

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:[email protected]
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
---------------------------------------------------------------------
 
I

iksrazal

John English said:
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
 
A

Andrea Desole

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
John English said:
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
 
I

iksrazal

Andrea Desole said:
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
 
A

Andrew Thompson

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

(shakes head) I missed it Thomas.

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? ;-)
 
T

Thomas Weidenfeller

Andrew said:
(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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top