Tianbo Zhang
Tianbo Zhang

Reputation: 27

In Java, how can I limit the shut down hook only support SIGTERM?

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 ..."); 
    } 
 } 

Upvotes: 0

Views: 544

Answers (1)

Koray Tugay
Koray Tugay

Reputation: 23844

I think what you are looking for is:

Signal.handle(new Signal("TERM"), sig -> {
  // handle sigterm    
});

Upvotes: 1

Related Questions