Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I cleanly shut down an embedded JRuby in response to a SIGTERM to the JVM process?

I am running a Middleman (which uses Webrick) server on JRuby inside a JVM process using the org.jruby.embed.ScriptingContainer API.

If I shutdown cleanly and stop the server from inside the JVM, everything works as expected.

But if I send a SIGTERM to the JVM process (for example, by hitting ctrl+C at the command line), the console returns but the JVM process does not terminate - it hangs around indefinitely until I send it a SIGKILL.

I tried registering a JVM shutdown hook to terminate the ScriptingContainer instance, but the hook never fires. I'm not sure why... perhaps JRuby is swallowing the SIGTERM somehow?

How can I get the JVM to shut all the way down, cleanly, even if it contains a running Webrick server?

like image 718
levand Avatar asked Apr 16 '15 15:04

levand


1 Answers

Looks like kill -9 does not trigger the shutdownHook, but kill -15 does.

This StackOverflow question has a detailed answer to the same problem.

Bottom line is, you're screwed, as there doesn't seem to exist any way to intercept a kill -9 signal and perform some maintenance tasks before the JVM halts.

like image 197
Olivier Croisier Avatar answered Sep 18 '22 12:09

Olivier Croisier