Tabular IQ

Overview

The Process class provides functionality for interacting with a system process.

Methods

Process.getPid
Returns the process id associated with a process.
Process.getStandardError
Retrieves the stderr stream of the process
Process.getStandardOutput
Retrieves the stdout stream of the process
Process.isRunning
Determines whether the process is running
Process.kill
Stops a system process
Process.setRedirect
Puts process object in redirect mode
Process.waitForExit
Waits for the process to terminate

Example

// Process runs an external program and can capture its output.
//
//   setRedirect(true)  must come before start(), or
//                      getStandardOutput()/getStandardError() give null
//   start()            returns at once, reports completion through the
//                      finished event, and so needs Application.run()
//   startAndWait()     blocks until the program exits
//
// Output is buffered while the program runs, so by the time finished
// fires the whole of it is there to read.

const process = new Process();

// capture the output; must be set before starting
process.setRedirect(true);

process.finished.connect(function(sender, event_args) {
    let output = `still running: ${sender.isRunning()}\n\n`;

    // getStandardOutput() returns a ProcessOutputStream; read it a line
    // at a time until eof().  getStandardError() is the same for stderr.
    const stream = sender.getStandardOutput();
    let count = 0;

    while (!stream.eof()) {
        const line = stream.readLine();
        if (line === null) {
            break;
        }

        count++;
        if (count <= 8) {
            output += `${line}\n`;
        }
    }

    alert(`${output}\n${count} line(s) of output`);

    Application.exit();
});

// on macOS or Linux this would be "/bin/sh -c env"
process.start("cmd /c set");

// getPid() is 0 if the program could not be launched -- in which case
// no finished event is coming.  kill() stops one that is still running.
if (process.getPid() === 0) {
    alert("could not start the process");
} else {
    // start() is asynchronous, so the event loop must run for finished
    // to arrive.  The blocking alternative is
    //
    //     process.startAndWait("cmd /c set");
    //
    // which returns only once the program has exited, letting the output
    // be read on the next line instead of from a handler.
    Application.run();
}

Process.getPid

function Process.getPid() : Integer

Returns

Returns the process id for the process represented by this object.

Description

This function returns the process id for the process represented by this object. If no process is associated with the object, the function returns 0.

Process.getStandardError

function Process.getStandardError() : ProcessOutputStream

Description

getStandardError() returns a ProcessOutputStream which allows the caller to retrieve the stderr output of a process.

Process.getStandardOutput

function Process.getStandardError() : ProcessOutputStream

Description

getStandardOutput() returns a ProcessOutputStream which allows the caller to retrieve the stdout contents of a process.

Process.isRunning

function Process.isRunning() : Boolean

Description

After starting a process with start(), calling isRunning() can be used to determine whether the process is still running or not

Process.kill

function Process.kill() : Boolean

Returns

True if the process is successfully stopped, and false otherwise.

Description

This function kills the process associated with this object.

Process.setRedirect

function Process.setRedirect(redirect : Boolean)

Description

Calling this method puts the process object in redirect mode. Redirct mode is useful for capturing the standard output and standard error output text of the process. A call to setRedirect() is necessary in order to use the getStandardOutput() and/or getStandardError() methods. setRedirect() must be called before start() is invoked.

Process.waitForExit

function Process.waitForExit()

Description

After starting a process with start(), calling waitForExit() will block the calling thread until the process has completed execution.