Handle TERM signal

25 views
Skip to first unread message

Brad Wood

unread,
Nov 10, 2020, 11:52:20 PM11/10/20
to jline...@googlegroups.com
Can JLine handle a SIGTERM with the signal handler?  I've used a signal handler for a long time to receive SIGINT like when Ctrl-C is pressed, but lately after doing more work on Docker with my JLine-powered console app, I noticed that when shutting down a docker container running a JVM CLI daemon process, a SIGTERM is used and I wanted to be able to interrupt my Main thread like I do with a SIGINT so everything can shutdown cleanly.  However, the SignalHandler doesn't seem to be called at all for a SIGTERM and the JVM just exits immediately. .  

If I register a shutdown hook on the JVM, I can accomplish this manually (interrupting the Main thread), but I wanted to make sure I wasn't missing a JLine way to do it.

Thanks!

~Brad

Developer Advocate
Ortus Solutions, Corp 

ColdBox Platform: http://www.coldbox.org 

Guillaume Nodet

unread,
Nov 11, 2020, 2:19:15 AM11/11/20
to jline...@googlegroups.com
The SIGTERM signal is missing from the JLine's Signal definition, so you won't be able to intercept it.
Simply adding the definition TERM enum to the Signal definition should work.  Is that what you tried?

Guilaume

--
You received this message because you are subscribed to the Google Groups "jline-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jline-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jline-users/CALbQ1omMC_9or%2BRBteSB5bzZppNjxd9Vn2UxDZviQhb1KgNR0A%40mail.gmail.com.

Brad Wood

unread,
Nov 11, 2020, 9:24:09 AM11/11/20
to jline...@googlegroups.com
I have no idea what "adding the definition TERM enum to the Signal definition" means, lol.  I just registered my own shutdown hook that interrupts the main thread and then waits up to 10 seconds for it to die before allowing the JVM to exit.  That way, any running process in my console app gets a chance to respond to the interrupt and shutdown when my docker containers are stopped.

  main = Thread.currentThread();
   
  Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {  
    public void run() {                
        main.interrupt();
        for (int i = 1; i <= 10; i++) {
            if( mainDone ) {
            break;
            }                    
            try {
                Thread.sleep(1000);
            } catch (Exception e) {}
        }
    }        
}));

 
Thanks!

~Brad

Developer Advocate
Ortus Solutions, Corp 

ColdBox Platform: http://www.coldbox.org 


Alejandro Revilla

unread,
Nov 11, 2020, 9:29:52 AM11/11/20
to jline...@googlegroups.com
You may want to consider calling thread's 'join(10000L)'.

Brad Wood

unread,
Nov 11, 2020, 11:23:26 AM11/11/20
to jline...@googlegroups.com
Thanks for the tip Alejandro.  I had some useful debugging information in the loop originally, but you have a good point, in its final form, there's no difference just joining to the thread with a 10 second timeout.

Thanks!

~Brad

Developer Advocate
Ortus Solutions, Corp 

ColdBox Platform: http://www.coldbox.org 


--
You received this message because you are subscribed to the Google Groups "jline-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jline-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages