11.5 Use Case: Running an External Process

If you need to invoke an external command, you must use the server.executeExternalProcess method. For example, you might want to create an Operations Center script to invoke a command line interface to send an automated email.

To invoke an external command:

  1. In the Configuration Manager, set up the daemon services by turning on the Daemon Shell Service Security, Daemon Shell Service Port, and Daemon Shell Service ACL.

  2. Ensure that ../logs/daemon.trc contains a message similar to the following:

    2016-05-15 12:17:39,782 INFO Daemon.Shell.Service - Susccessfully started service on port 1555 using security level unsecured

  3. Create a server side operation called Ping Test Script:

    var result = server.executeExternalProcess( [ 'ping', '<host>'], [ 'PATH', '<path to the ping command>']); formula.log.info( result.getOutputAsSingleString());

  4. Run the operation and check for the ping output in ../logs/formula.trc.

  5. Create the desired operation.

The server object has two methods available for calling processes externally: ProcessResult executeExternalProcess(String command[], String systemVars[]) and ProcessResult executeExternalProcess(String host, int port, String command[], String systemVars[]).

Commands must be passed as an array of strings, delimited by blanks. For example:

var command = ['ping', 'turkey'];
var command = ['ping', '-a', 'turkey'];
executeExternalProcess( ['ping', '-a', 'turkey'], []);

Environment variables are optional, but always pass an empty array. For example:

var env = ['PATH', '/usr/local/bin'];
var env = ['PATH', '/usr/local/bin:/usr/bin'];
var env = ['PATH', '/usr/local/bin:/usr/bin', 'LD_LIBRARY_PATH', '/usr/lib'];
executeExternalProcess( ['ping', '-a', 'turkey'], ['PATH', '/usr/local/bin:/usr/bin', 'LD_LIBRARY_PATH', '/usr/lib']);

The ProcessResult method returns the value for any call to the executeExternalProcess method. For example:

String [] getErrors();
String getErrorsAsSingleString();
String[] getOutput();
String getOutputAsSingleString();
boolean hasErrors();
boolean hasOutput();
int returnCode();