SBT stop run without exiting

ScalaSbtTerminate

Scala Problem Overview


How do you terminate a run in SBT without exiting?

I'm trying CTRL+C but it exits SBT. Is there a way to only exit the running application while keeping SBT open?

Scala Solutions


Solution 1 - Scala

From sbt version 0.13.5 you can add to your build.sbt

cancelable in Global := true

It is defined as "Enables (true) or disables (false) the ability to interrupt task execution with CTRL+C." in the Keys definition

If you are using Scala 2.12.7+ you can also cancel the compilation with CTRL+C. Reference https://github.com/scala/scala/pull/6479

There are some bugs reported:

Solution 2 - Scala

In the default configuration, your runs happen in the same JVM that sbt is running, so you can't easily kill them separately.

If you do your run in a separate, forked JVM, as described at Forking, then you can kill that JVM (by any means your operating system offers) without affecting sbt's JVM:

fork in run := true

Solution 3 - Scala

I've found the following useful when I have control over the main loop of the application being run from sbt.

I tell sbt to fork when running the application (in build.sbt):

fork in run := true

I also tell sbt to forward stdin from the sbt shell to the application (in build.sbt):

connectInput in run := true

Finally, in the main thread of the application, I wait for end-of-file on stdin and then shutdown the JVM:

while (System.in.read() != -1) {}
logger.warn("Received end-of-file on stdin. Exiting")
// optional shutdown code here
System.exit(0)

Of course, you can use any thread to read stdin and shutdown, not just the main thread.

Finally, start sbt, optionally switch to the subproject you want to run, run.

Now, when you want to stop the process, close its stdin by typing CTRL-D in the sbt shell.

Solution 4 - Scala

Consider using sbt-revolver. We use it in our company and it's really handy. For what you're asking can be done with:

reStart

reStop

Without need to configure build.sbt file.

Your can use this plugin by adding:

addSbtPlugin("io.spray" % "sbt-revolver" % "0.9.1")

To your project/plugins.sbt

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondsgView Question on Stackoverflow
Solution 1 - ScalaJonas AnsoView Answer on Stackoverflow
Solution 2 - ScalaSeth TisueView Answer on Stackoverflow
Solution 3 - ScalakiloView Answer on Stackoverflow
Solution 4 - ScalamtsokolView Answer on Stackoverflow