scala.sys

process

package process

This package is used to create process pipelines, similar to Unix command pipelines.

The key concept is that one builds a Process that will run and return an exit value. This Process is usually composed of one or more ProcessBuilder, fed by a Source and feeding a Sink. A ProcessBuilder itself is both a Source and a Sink.

As ProcessBuilder, Sink and Source are abstract, one usually creates them with apply methods on the companion object of Process, or through implicit conversions available in this package object from String and other types. The pipe is composed through unix-like pipeline and I/O redirection operators available on ProcessBuilder.

The example below shows how to build and combine such commands. It searches for null uses in the src directory, printing a message indicating whether they were found or not. The first command pipes its output to the second command, whose exit value is then used to choose between the third or fourth commands. This same example is explained in greater detail on ProcessBuilder.

import scala.sys.process._
(
    "find src -name *.scala -exec grep null {} ;"
    #|  "xargs test -z"
    #&&  "echo null-free"  #||  "echo null detected"
) !

Other implicits available here are for FileBuilder, which extends both Sink and Source, and for URLBuilder, which extends Source alone.

One can even create a Process solely out of these, without running any command. For example, this will download from a URL to a file:

import java.io.File
import java.net.URL
import scala.sys.process._
new URL("http://www.scala-lang.org/") #> new File("scala-lang.html") !

One may use a Process directly through ProcessBuilder's run method, which starts the process in the background, and returns a Process. If background execution is not desired, one can get a ProcessBuilder to execute through a method such as !, lines, run or variations thereof. That will create the Process to execute the commands, and return either the exit value or the output, maybe throwing an exception.

Finally, when executing a ProcessBuilder, one may pass a ProcessLogger to capture stdout and stderr of the executing processes. A ProcessLogger may be created through its companion object from functions of type (String) => Unit, or one might redirect it to a file, using FileProcessLogger, which can also be created through ProcessLogger's object companion.

Visibility
  1. Public
  2. All

Type Members

  1. class FileProcessLogger extends ProcessLogger with Closeable with Flushable

    A ProcessLogger that writes output to a file.

  2. trait Process extends AnyRef

    Represents a process that is running or has finished running.

  3. trait ProcessBuilder extends Source with Sink

    Represents a runnable process.

  4. trait ProcessCreation extends AnyRef

    Factories for creating ProcessBuilder.

  5. final class ProcessIO extends AnyRef

    This class is used to control the I/O of every ProcessBuilder.

  6. trait ProcessImplicits extends AnyRef

    Provide implicit conversions for the factories offered by Process's companion object.

  7. trait ProcessLogger extends AnyRef

    Encapsulates the output and error streams of a running process.

Value Members

  1. object BasicIO extends AnyRef

    This object contains factories for ProcessIO, which can be used to control the I/O of a Process when a ProcessBuilder is started with the run command.

  2. object Process extends ProcessImpl with ProcessCreation

    Methods for constructing simple commands that can then be combined.

  3. object ProcessBuilder extends ProcessBuilderImpl

    This object contains traits used to describe input and output sources.

  4. object ProcessLogger extends AnyRef

    Provides factories to create ProcessLogger, which are used to capture output of ProcessBuilder commands when run.

  5. implicit def builderToProcess(builder: JProcessBuilder): ProcessBuilder

    Implicitly convert a java.lang.ProcessBuilder into a Scala one.

    Implicitly convert a java.lang.ProcessBuilder into a Scala one.

    Definition Classes
    ProcessImplicits
  6. implicit def buildersToProcess[T](builders: Seq[T])(implicit convert: (T) ⇒ Source): Seq[Source]

    Return a sequence of Source from a sequence of values for which an implicit conversion to Source is available.

    Return a sequence of Source from a sequence of values for which an implicit conversion to Source is available.

    Definition Classes
    ProcessImplicits
  7. implicit def fileToProcess(file: File): FileBuilder

    Implicitly convert a java.io.File into a ProcessBuilder

    Implicitly convert a java.io.File into a ProcessBuilder

    Definition Classes
    ProcessImplicits
  8. def javaVmArguments: List[String]

    Definition Classes
    package
  9. def stderr: PrintStream

    Definition Classes
    package
  10. def stdin: InputStream

    Definition Classes
    package
  11. def stdout: PrintStream

    Definition Classes
    package
  12. implicit def stringSeqToProcess(command: Seq[String]): ProcessBuilder

    Implicitly convert a sequence of String into a ProcessBuilder

    Implicitly convert a sequence of String into a ProcessBuilder

    Definition Classes
    ProcessImplicits
  13. implicit def stringToProcess(command: String): ProcessBuilder

    Implicitly convert a String into a ProcessBuilder

    Implicitly convert a String into a ProcessBuilder

    Definition Classes
    ProcessImplicits
  14. implicit def urlToProcess(url: URL): URLBuilder

    Implicitly convert a java.net.URL into a ProcessBuilder

    Implicitly convert a java.net.URL into a ProcessBuilder

    Definition Classes
    ProcessImplicits
  15. implicit def xmlToProcess(command: Elem): ProcessBuilder

    Implicitly convert a Elem into a ProcessBuilder

    Implicitly convert a Elem into a ProcessBuilder

    Definition Classes
    ProcessImplicits