The Process class provides functionality for interacting with a system process.
// 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();
} Returns the process id for the process represented by this object.
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.
getStandardError() returns a ProcessOutputStream which allows the caller to retrieve the stderr output of a process.
getStandardOutput() returns a ProcessOutputStream which allows the caller to retrieve the stdout contents of a process.
After starting a process with start(), calling isRunning() can be used to determine whether the process is still running or not
True if the process is successfully stopped, and false otherwise.
This function kills the process associated with this object.
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.
After starting a process with start(), calling waitForExit() will block the calling thread until the process has completed execution.