자바에서 프로그램으로 kill 시그널 캡쳐방법과 종료시 메소드 호출(소멸자 같은)하는 후킹 방법
import sun.misc.Signal;
import sun.misc.SignalHandler;
public class Test {
public Test() {
Runtime.getRuntime().addShutdownHook(new ShutdownHookThread());
catchSig();
}
public static void main(String[] args) {
Test test = new Test();
try {
Thread.sleep(1000);
/*종료시킨다.*/
//System.exit(0);
System.exit(1);
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public class ShutdownHookThread extends Thread {
public ShutdownHookThread() {
System.out.println("HOOKed");
}
public void run() {
System.out.println("ShutdownHookThread Called Successfully!!!!");
}
}
public void catchSig(){
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++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.print('.');
}
}
}