Exit code upon SIGTERM #29582
-
I’d like to know how I can tell Quarkus to gracefully exit with code 0 upon receiving SIGTERM. I’d still like to react differently (i.e. with an “error” exit code, meaning any number strictly greater than 0) to other exit conditions (such as receiving another signal than SIGTERM). I have read Graceful Shutdown but it does not talk about specifically reacting to SIGTERM. I suppose that this might involve The reason for this question is that the free hosting plan of Render sends SIGTERM to applications after 15 minutes of inactivity, and Render will consider it an error if the application exits with a non-zero code and will log it accordingly (and, perhaps, react differently). I’d like to distinguish error conditions from usual, expected activity. Question also asked on SO. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
@dmlloyd can you shed light on ways to handle this signal handling? |
Beta Was this translation helpful? Give feedback.
-
In native modeQuarkus typically installs four signal handlers when starting up in native mode:
On Windows, The exit signal handler follows the UNIX standard of exiting the program with If you set the environment variable In JVM modeIn JVM mode we do not install any signal handlers (at least I don't believe we do). Overriding behaviorsIf you're in native mode, disable the signal handlers as above first, otherwise nothing here will have an effect. To install your own signal handler you'll need to provide your own The actual code is quite simple: package myapp;
import io.quarkus.runtime.Quarkus;
import sun.misc.Signal;
public class Main {
public static void main(String[] args) {
Signal.handle(new Signal("TERM"), sig -> System.exit(0));
// ... continue with normal startup ...
Quarkus.run(args);
}
} There is more info on providing a main method in the documentation. Note that |
Beta Was this translation helpful? Give feedback.
-
Thank you for the answer. I suppose that I use the JVM mode as I start Quarkus using the I use the |
Beta Was this translation helpful? Give feedback.
-
Ok, I had not understood that point. I intend to use the native mode at some point though (currently not just for simplicity), so my question remains about using the |
Beta Was this translation helpful? Give feedback.
In native mode
Quarkus typically installs four signal handlers when starting up in native mode:
INT
- exit the programTERM
- exit the programHUP
- exit the programQUIT
- print diagnosticsOn Windows,
HUP
andQUIT
do not exist; so, on this platform we register the pseudo-signal calledBREAK
which prints diagnostics.The exit signal handler follows the UNIX standard of exiting the program with
128
plus the signal number.If you set the environment variable
DISABLE_SIGNAL_HANDLERS
totrue
, the signal handlers will not be installed and the default will be used instead (though I believe the default has similar exit code semantics).In JVM mode
In JVM mode we do not install any signal hand…