Executing External ProcessesYou create processes that call out to native operating system commands as well as applications and scripts written in other languages. Process execution is significantly operating system dependent and resource dependent and you should not expect every process to be able to run cross-platform with reliability. Execute a ProcessThe simplest way to execute a process is with a single line: Jaxer.Process.exec("pathToExecutable", "argument1", "argument2");
This could also be written in an object literal form:
Jaxer.Process.exec({
path : "pathToExecutable",
args : [
"argument1",
"argument2"
]
});
Configure a ProcessProcess Options
The
Jaxer.Process.exec("pathToExecutable", "argument1", "argument2", {
async : true
});
The entire method can also, as shown before, be configured using object literal notation:
Jaxer.Process.exec({
path : "pathToExecutable",
args : [
"argument1",
"argument2"
], {
async : true
}
});
Configure Input and OutputYou can set up the process options to provide input and capture output.
var input = "";
var output = "";
var errors = "";
var options = {
stdin : input,
stdout : output,
stderr : errors
}
Jaxer.Process.exec({
path : "pathToExecutable",
args : [
"argument1",
"argument2"
],
options
});
Jaxer.Log.info("output: " + output);
Jaxer.Log.info("errors: " + errors);
Interact with a Process
Being able to set up input to a process is particularly useful when you instantiate a process in a way that gives you more fine-grained
control over it through repeated interactions. For synchronously executed processes you can also capture the exit status (return code)
by reading the
var process = new Jaxer.Process();
process.path = 'C:\\opt\\perl\\bin\\perl.exe';
process.beginExec('-ne printf("%02d %s", ++$i, $_);');
process.writeString('Some text\nto format.');
process.endWrite();
var result = process.readString();
process.endExec();
var res = process.exitStatus;
if(res > 0) {
Jaxer.Log.info("result: " + result);
} else {
Jaxer.Log.error("error code: " + res);
}
|
