12

I have an SBT scala application that runs fine using "sbt run". However, this locks up the console, and I'd rather start it as a service/daemon so that I can use the console, and also so that I can add it to init.d to ensure that my application is started automatically on startup.

I can't seem to find a way to do this. Running "sbt run &" seems to hang the app in the background.

Does anybody know how to do this?

devinfoley
  • 1,896
  • 16
  • 20

4 Answers4

9

You could also jar up your application into a "fat" jar using either sbt-assembly or sbt-onejar.

This will make it an executable jar and easily runnable via java -jar jarname.jar.

charego
  • 137
  • 9
Luke Amdor
  • 91
  • 2
  • I guess I was hoping this would be built into SBT so that I could skip the package step, but I'll look into these. – devinfoley May 16 '11 at 04:03
8

We launch test/demo apps with SBT in init.d all the time:

#!/bin/sh
# test lift web app

case "$1" in
'start')
    cd /home/demouser/wa/HTML5DemoLift231/HTML5demo/
    sbt jetty run
    ;;
'stop')
    cd /home/demouser/wa/HTML5DemoLift231/HTML5demo/
    sbt jetty stop
    ;;
*)
    echo "Usage: $0 { start | stop }"
    ;;
esac
exit 0

This just works - we have had no problems with it. It may be different with a non-web app though.

Johan Prinsloo
  • 1,188
  • 9
  • 9
  • Thanks. Changing this to the correct answer. This eliminates the package step and allows me to deploy faster. – devinfoley May 17 '11 at 14:59
  • 5
    It does starts but quits immediately. In my case I'm using sbt container:start. Any ideas? – Johnny Everson Feb 08 '12 at 15:38
  • 1
    Same here. It starts but then quits immediately. Any ideas? – Jack Mar 08 '12 at 14:52
  • 2
    @JhonnyEverson @JacobusR `sbt container:start shell` works around this by starting a command line shell after the container. But what's the correct way to do this? (Not use `sbt` directly, I suppose.) – wodow Oct 18 '13 at 21:49
  • @wodow That's really useful. You want to answer my question here: http://stackoverflow.com/questions/32544032/how-to-keep-sbt-running-after-executing-a-command/32544892?noredirect=1#comment52946920_32544892 – WestCoastProjects Sep 13 '15 at 05:01
6

You can use GNU Screen for keeping it in background. Anyway I can't think a good reason to do that. Whouldn't it be better to package the application and run the generated binaries in the background?

Rafa de Castro
  • 2,474
  • 1
  • 24
  • 44
2

Just type sbt runProd And then press Ctrl+D The process will be running as a daemon process.

Karan Sharma
  • 475
  • 1
  • 5
  • 16