How to run external process in Scala and get both exit code and output?

run shell command from scala code
scala execute string as code
show command in scala
sbt run bash command
scala process output
exec scala not found
spark run bash script
scala get current process id

How to call an external process and read both of its exit code and standard out, once it finished?

Using sys.Process will result in an exception being thrown on different exit code than 0 for success.

Try this:

import sys.process._

val stdout = new StringBuilder
val stderr = new StringBuilder
val logger = ProcessLogger(stdout append _, stderr append _)
val status = "ls -al " ! logger

println(status)
println("stdout: " + stdout)
println("stderr: " + stderr)

Then you got both of them: status, stdout and stderr.

How to execute external commands and use their STDOUT in Scala , How to call an external process and read both of its exit code and standard out, once it Have you looked at Process. exitValue() val output = scala.io.Source. Executing system commands and getting their status code (exit code) It's very easy to run external system commands in Scala. You just need one import statement, and then you run your command as shown below with the "!" operator: scala> import sys.process._ import sys.process._ scala> "ls -al" ! total 64 drwxr-xr-x 10 Al staff 340 May 18 18:00 . drwxr-xr-x 3 Al staff 102 Apr 4 17:58 ..

Have you looked at Process.exitValue?

Returns the exit value for the subprocess.

Scala Standard Library 2.13.2, Use the ! method to get the exit code from a process, or !! to get the standard output from a process. Be aware that attempting to  This is Recipe 12.12, “How to execute external commands and use their STDOUT in Scala.” Problem. You want to run an external command and then use the standard output (STDOUT) from that process in your Scala program. Solution. Use the !! method to execute the command and get the standard output from the resulting process as a String.

(I've asked this question on freenode #java and was requested to post here if I found a solution, so here goes)

Simple approach is to use sys.ProcessBuilder:

  def RunExternal(executableName: String, executableDir: String) : (Int, List[String]) = {
    val startExecutionTime = System.currentTimeMillis()

    val pb : ProcessBuilder = new ProcessBuilder (executableName)
    pb.directory(new java.io.File(executableDir))

    val proc = pb.start()
    proc.waitFor()

    val exitCode = proc.exitValue()
    val output = scala.io.Source.fromInputStream(proc.getInputStream).getLines.toList

    val executionTime = System.currentTimeMillis() - startExecutionTime

    logger.info(String.format(s"Process exited with exit code: ${exitCode}."))
    logger.info(String.format(s"Process took ${executionTime} milliseconds."))

    (exitCode, output)
  }

scala.sys.process.ProcessBuilder, How to call an external process and read both of its exit code and standard out, get the exit code from a process, or !! to get the standard output from a process. To execute external commands, use the methods of the scala.sys.process package. There are three primary ways to execute external commands: Use the ! method to execute the command and get its exit status. Use the !! method to execute the command and get its output. Use the lines method to execute the command in the background and get its result as a Stream.

12.12. Handling STDOUT and STDERR for External Commands , Running an external command can be as simple as "ls".! , or as complex as building Return status of the process ( ! methods); Output of the process as a String _ // This uses ! to get the exit code def fileExists(name: String) = Seq("test​", "-f", name). URL can both be used directly as input to other processes, and java.io. Indicating what to run and how to run it. Handling a process input and output. Running the process. For simple uses, the only group that matters is the first one. Running an external command can be as simple as "ls".!, or as complex as building a pipeline of commands such as this:

12.10. Executing External Commands, One can control where a the output of an external process will go to, and where its input Execute "ls" and assign a `Stream[String]` of its output to "contents". two ProcessBuilder to create a third, the ones that redirect input or output of a blocks until all external commands exit, and returns the exit code of the last one in  scala.sys.process package process. This package handles the execution of external processes. The contents of this package can be divided in three groups, according to their responsibilities: Indicating what to run and how to run it. Handling a process input and output. Running the process. For simple uses, the only group that matters is the

ProcessBuilder, You want to run an external command and get access to both its STDOUT and STDERR . This Scala shell script demonstrates the approach: Mac OS X (​Unix) system, I correctly get the following exit status, STDOUT , and STDERR output: Here's a really simple Scala wrapper that allows you to retrieve stdout, stderr and exit code. import scala.sys.process._ case class ProcessInfo(stdout: String, stderr: String, exitCode: Int) object CommandRunner { def runCommandAndGetOutput(command: String): ProcessInfo = { val stdout = new StringBuilder val stderr = new StringBuilder val

Comments
  • Thank you; simpler and more Scalaesque than the accepted solution, and worked well for me.
  • yes, however, bare Process throws on supposed exitValue being other than 0, I'm not sure whether I can get both output and exit code with it? (I've changed my approach to ProcessBuilder, which solved the issue using the function you suggested)