127

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?

Oleg Mirzov
  • 155
  • 3
  • 8
dsg
  • 12,924
  • 21
  • 67
  • 111

4 Answers4

85

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:

Jonas Anso
  • 2,057
  • 14
  • 13
49

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:

run / fork := true
Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
  • 9
    I'm trying to do this. Specifically, I've implemented: http://stackoverflow.com/questions/3868863/how-to-specify-jvm-maximum-heap-size-xmx-for-running-an-application-with-run/4054152#4054152 but sbt still exits when I do CTRL+C. How do I kill just the forked JVM? – dsg Mar 20 '11 at 08:00
  • 2
    with `kill` from the command line, or in the Task Manager (Windows), or Force Quit or Activity Monitor (Mac OS X), etc. – Seth Tisue Mar 21 '11 at 16:45
  • After forking, to kill everything java except sbt, run: `kill -9 \`ps -h | grep java | grep -v sbt-launch | grep -v grep | awk '{print $1}'\`` – dsg Nov 27 '12 at 21:05
  • 1
    I love this idea, but it doesn't seem to help. I'm running sbt 0.13.1 and adding this line into my build.sbt file.. – doub1ejack Nov 10 '14 at 17:03
  • you need to kill the `run` with CTRL+D – Timofey Apr 10 '15 at 20:03
  • control-D doesn't help for me in my scenario - no response. The fork in run option is an interesting workaround -- I would prefer the simplicity of being able to use control-c. – ether_joe Jun 12 '15 at 01:18
8

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.

kilo
  • 539
  • 4
  • 13
1

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

mtsokol
  • 170
  • 1
  • 12