0

I found that the runtime shutdown hook in Java can handle SIGTERM, SIGHUP, SIGINT and normal exit(0). However, I only want the hook to handle the SIGTERM signal. How can I limit it? Simple example from geeksforgeeks:

public class ShutDownHook 
{ 
   public static void main(String[] args) 
   { 

     Runtime.getRuntime().addShutdownHook(new Thread() 
     { 
         public void run() 
         { 
             System.out.println("Shutdown Hook is running !"); 
         } 
     }); 
     System.out.println("Application Terminating ..."); 
    } 
 } 
Tianbo Zhang
  • 27
  • 1
  • 4

1 Answers1

1

I think what you are looking for is:

Signal.handle(new Signal("TERM"), sig -> {
  // handle sigterm    
});
Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
  • I knew this, but I found someone said using signal handler would cause some corner case. Actually, I have set up the shut down hook. It would be easier to modify the old one if I can get the signal. If I cannot get the signal, I would use signal handler~ – Tianbo Zhang May 28 '20 at 01:58