Question

I'm writing a server that runs in a loop and terminates in case of SIGINT(ctrl-c) or terminate button pressed in the console window of Eclipse IDE. I want it to shutdown gracefully printing out termination logs. But the problem is that println doesn't seem to work while shutdown sequence triggered by pressing the terminate button in Eclipse IDE. Look at the simple code below:

object Test extends App {
  println("start")
  Runtime.getRuntime().addShutdownHook(new Thread {
    override def run = println("shutdown")
  })
  synchronized { wait }
}

It works well with the command line scala tool. Both messages "start" and "shutdown" are printed when I hit ctrl-c. But the "shutdown" message isn't printed when I run it in Eclipse IDE and hit the terminate button of the console window. It just terminates silently. I've verified that everything else in the shutdown hook runs correctly while termination. It's only println that doesn't work.

Any idea about this? I need to print out termination messages for logging.
Thanks for your help in advance!

Was it helpful?

Solution

Try logging the shutdown messages to a file instead. Eclipse probably closes its end of the stdout pipe before the program is really terminated.

OTHER TIPS

Shutdown hooks don't get run by Eclipse when shutting down a process: https://bugs.eclipse.org/bugs/show_bug.cgi?id=38016

That bug report is resolved as Won't fix

I realise that its an old bug and so maybe things have changed since then.

You can try the shutDownHook which scala provides

sys addShutdownHook(println("shutdown")) 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top