keypoint keypoint - 1 year ago 69
Scala Question

scala.sys.process.Process("ls").run().exitValue() with no output

When I run Process(command), the output of the command is printed out to terminal, but I only want the exitValue().

Is there a way to suppress the output displayed on terminal? I just find sometime the command processed could print a huge output result on terminal. Thanks a lot.

scala> import scala.sys.process._
import scala.sys.process._

scala> Process("ls").run().exitValue()
res6: Int = 0

Answer Source

Use the run() overload that takes a ProcessLogger. Implement a ProcessLogger that ignores strings passed to out and err:

Process("ls").run(new ProcessLogger {
  override def out(s: => String): Unit = ()
  override def buffer[T](f: => T): T = f
  override def err(s: => String): Unit = ()