# Process Service

The `Process` service allows you to start processes, track their output, and so on.

## Making the Service Available

In order to gain access to process management operations, you need to import the service using the following statement at the top of your project file:

    import qbs.Process

## Available Operations

### Constructor

    Process()

Allocates and returns a new Process object.

### close

    close()

Frees the resources associated with the process. We recommended to always call this function as soon as you are finished with the process.

### exec

    exec(filePath, arguments, throwOnError)

Executes the program at `filePath` with the given argument list and blocks until the process is finished. If an error occurs (for example, there is no executable file at `filePath`) and `throwOnError` is true, then a JavaScript exception will be thrown. Otherwise (the default), -1 will be returned in case of an error. The normal return code is the exit code of the process.

### exitCode

    exitCode()

Returns the exit code of the process. This is needed for retrieving the exit code from processes started via `start()`, rather than `exec()`.

### getEnv

    getEnv(varName)

Returns the value of the variable `varName` in the process' environment.

### kill

    kill()

Kills the process, causing it to exit immediately.

### readLine

    readLine()

Reads and returns one line of text from the process output, without the newline character(s).

### readStdErr

    readStdErr()

Reads and returns all data from the process' standard error channel.

### readStdOut

    readStdOut()

Reads and returns all data from the process' standard output channel.

### setEnv

    setEnv(varName, varValue)

Sets the value of variable `varName` to `varValue` in the process environment. This only has an effect if called before the process is started.

### setWorkingDirectory

    setWorkingDirectory(dir)

Sets the directory the process will be started in. This only has an effect if called before the process is started.

### start

    start(filePath, arguments)

Starts the program at `filePath` with the given list of arguments. Returns true if the process could be started and false otherwise.

Note: This call returns right after starting the process and should be used only if you need to interact with the process while it is running. Most of the time, you want to use `exec()` instead.

### terminate

    terminate()

Tries to terminate the process. This is not guaranteed to make the process exit immediately; if you need that, use `kill()`.

### waitForFinished

    waitForFinished(timeout)

Blocks until the process has finished or `timeout` milliseconds have passed (default is 30000). Returns true if the process has finished and false if the operation has timed out. Calling this function only makes sense for processes started via `start()` (as opposed to `exec()`).

### workingDirectory

    \workingDirectory()

Returns the directory the process will be started in.

### write

    write(data)

Writes `data` into the process' input channel.

### writeLine

    writeLine(data)

Writes `data`, followed by the newline character(s), into the process' input channel.