Executing External Processes

You 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 Process

The 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 Process

Process Options

OptionDescription
pathpath
argsargs
asyncasync
stdinstdin
stdoutstdout
stderrstderr

The exec method can be configured by passing a final object parameter. As the next example shows,you can execute a process asynchronously by setting the async property to true (you could also do so by calling execAsync() rather than exec()).

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 Output

You 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 existStatus property after it runs.

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);
}