1

I'm trying to run a daemon in the scala console in the background. I can get it to run the program but it locks the console window when it is running, and therefore forces me to use a separate window to stop the daemon to unlock the original console. I'm running the scala console in a windows powershell through sbt.

I can use the command prompt to successfully run the program in the background using: start /b program, but running ("start /b program").! in the scala console fails.

This will run the program in scala console but will lock the window: ("cmd /c start program").!

How can I get the program to successfully run in the background so I still have access to the current console? I've been fiddling with /E:ON using /b as an extension of start to no avail.

These are results from cmd /?

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\>cmd /?
Starts a new instance of the Windows XP command interpreter

CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V:OFF]
    [[/S] [/C | /K] string]

/C      Carries out the command specified by string and then terminates
/K      Carries out the command specified by string but remains
/S      Modifies the treatment of string after /C or /K (see below)
/Q      Turns echo off
/D      Disable execution of AutoRun commands from registry (see below)
/A      Causes the output of internal commands to a pipe or file to be ANSI
/U      Causes the output of internal commands to a pipe or file to be
        Unicode
/T:fg   Sets the foreground/background colors (see COLOR /? for more info)
/E:ON   Enable command extensions (see below)
/E:OFF  Disable command extensions (see below)
/F:ON   Enable file and directory name completion characters (see below)
/F:OFF  Disable file and directory name completion characters (see below)
/V:ON   Enable delayed environment variable expansion using ! as the
        delimiter. For example, /V:ON would allow !var! to expand the
        variable var at execution time.  The var syntax expands variables
        at input time, which is quite a different thing when inside of a FOR
        loop.
/V:OFF  Disable delayed environment expansion.
  • Usually, just exec the process, "foo".! But XP? What decade is this? – som-snytt Jan 20 '16 at 02:17
  • @som-snytt I'm running Windows 10, idk why it says that. Also that will run the program but not in the background like I want. =/ I know the PATH exists as well. – chickachickayeah Jan 20 '16 at 02:43
  • Ha. "Background" in this context just means "don't block this thread." And stdio for the process is not console. The docs for `sys.process` package and `ProcessBuilder` show how, `run(ProcessIO)`. I'll add a SO entry if I find one. – som-snytt Jan 20 '16 at 06:04
  • http://stackoverflow.com/a/6013972/1296806 – som-snytt Jan 20 '16 at 06:06
  • @som-snytt that SO helped tremendously. Sorry my descriptions are rough. I'm still a novice. Thank you! – chickachickayeah Jan 21 '16 at 19:52

2 Answers2

1

You should use forking in SBT, that's exactly what it is for.

For your case set this setting in your SBT build:

fork in run := true

By default stdin will not be connected to your forked process, to enable it do:

connectInput in run := true
yǝsʞǝla
  • 16,272
  • 2
  • 44
  • 65
0

Figured it out thanks to @som-snytt's link to @michael.kebe's answer on this SO entry: How does the “scala.sys.process” from Scala 2.9 work?

val pb = Process("""program""").run

Does exactly what I wanted

Community
  • 1
  • 1