Skip to content

infinity.process

Module: infinity.process

Provides functionality for starting external programs and for checking, waiting for and killing running processes.

Note: exists(), kill() and waitFor() are only implemented on Windows; on other platforms they always return false. On Linux, start() only works with waitfor = true (see start()).

Example:

infinity.loadModule('infinity.process');

infinity.process.start('C:/Program Files/Internet Explorer/iexplore.exe', 'https://localhost');
infinity.sleep(3000);
console.debug(infinity.process.kill('iexplore.exe'));
infinity.loadModule('infinity.process');

let result = infinity.process.start(infinity.process.which('git'), '--version', true, 10);

if (result.success && result.exitCode === 0) {
    console.info(result.output);
}

Interfaces

result

An object according to this interface is being returned by the start function.

Properties:


Properties


exitCode

Type: number

Gets the exit code of the started process (since 3.10.177 also under Linux). Only meaningful if start() has been called with waitfor = true and success is true.


output

Type: string

Gets the complete output of the started process, if start() has been called with waitfor = true (Windows: standard output and standard error, Linux: standard output only; append 2>&1 to the parameters to include standard error). Empty otherwise.


success

Type: boolean

Indicates whether the process could be started and, if waitfor = true, has finished (on Windows false if the process was terminated because of the timeout or the termination of INFINITY.JS).


Functions

correctCmdLineParam()

Quotes the string if necessary to ensure that it is interpreted as a single command line parameter: if it contains spaces, it is enclosed in double quotes (double quotes inside the string are doubled). Otherwise the string is returned unchanged.

Signature:

correctCmdLineParam( param: string ): string

Parameters:

  • param: string

    The string to be corrected.

Return type: string

Example:

infinity.loadModule('infinity.process');
let s = infinity.process.correctCmdLineParam('C:/My Files/test.txt');
//"C:/My Files/test.txt"

exists()

Specifies, whether there is a process with the provided name (file name of the executable, case-insensitive, e.g. 'notepad.exe') running in the moment of the function call. Windows only; always returns false on other platforms.

Signature:

exists( processName: string ): boolean

Parameters:

  • processName: string

    The process name to check for.

Return type: boolean

Example:

infinity.loadModule('infinity.process');
if ( infinity.process.exists('iexplore.exe') ) {
  //...
}

kill()

Attempts to kill all processes with the specified name (file name of the executable, case-insensitive) including their child processes, and waits until they have ended. Returns false if killing a process failed. Note that true is also returned if no process with this name was running. Windows only; always returns false on other platforms.

Signature:

kill( processName: string ): boolean

Parameters:

  • processName: string

    The name of the process to kill.

Return type: boolean

Example:

infinity.loadModule('infinity.process');
if ( infinity.process.kill('iexplore.exe') ) {
  //...
}

start()

Launches the specified executable file as a process with optionally provided parameters. Returns an object with result information according to the infinity.process.result interface.

Platform differences:

  • Windows: a bare program name (e.g. node) is searched like the shell does it, see which(); .cmd and .bat files are run through cmd.exe. The working directory is the folder of fileName if it contains a path, otherwise the current directory. Output in UTF-8 is detected, other output is decoded with the OEM code page. If INFINITY.JS is being terminated while waiting, the process is killed.
  • Linux: the command line is executed by the shell (popen), so PATH is searched and shell syntax is possible. The process is only started if waitfor is true; with waitfor = false nothing is started and success is false. The timeout parameter is ignored, and the working directory is not changed.

Signature:

start( fileName: string, params?: string, waitfor?: boolean, timeout?: number ): infinity.process.result

Parameters:

  • fileName: string

    Filename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the executable file which should be launched. The file name is quoted automatically.

  • params: string, optional

    Command line parameters which should be provided to the started process, appended to the quoted file name. Use correctCmdLineParam() to quote single parameters.

  • waitfor: boolean, optional

    Specifies whether the function should wait for the launched process to complete, before returning its results (exit code and output). Default: false.

  • timeout: number, optional

    Windows only: maximum number of seconds to wait for the process if waitfor is true. If the process is still running after this time, it is killed and success is false. 0 or omitted: wait without time limit.

Return type: infinity.process.result

Example:

infinity.loadModule('infinity.process');
let resultObject = infinity.process.start('C:/Program Files/Internet Explorer/iexplore.exe', 'https://localhost');
infinity.loadModule('infinity.process');
let result = infinity.process.start('/usr/bin/ocrmypdf', infinity.process.correctCmdLineParam(inputFile) + ' ' +
    infinity.process.correctCmdLineParam(outputFile) + ' 2>&1', true);
console.info(result.exitCode + ': ' + result.output);

waitFor()

Waits for the given process (file name of the executable, case-insensitive) to finish or terminate. Script execution is paused until no process with this name is running anymore or the specified number of seconds has passed. Returns true if no process with this name is running (anymore), false on timeout. Windows only; always returns false on other platforms.

Signature:

waitFor( processName: string, waitFor: number ): boolean

Parameters:

  • processName: string

    The name of the process to wait for.

  • waitFor: number

    Number of seconds to wait for the process to finish or terminate (attention: this parameter is a number of seconds, not milliseconds). 0 waits without time limit.

Return type: boolean

Example:

infinity.loadModule('infinity.process');
if ( infinity.process.waitFor('iexplore.exe', 5) ) {
  //...
}

which()

Returns the full path of an executable, found the way the shell would find it: a name containing a path is checked as is, a bare name is searched in the directories of the PATH environment variable (on Windows, a name without extension is only found with one of the extensions of PATHEXT, like npm.cmd for npm). Returns an empty string if the executable is not found. The path is returned with forward slashes. Since 3.10.174.

Signature:

which( fileName: string ): string

Parameters:

  • fileName: string

    The name or path of the executable, e.g. 'node' or 'git.exe'.

Return type: string

Example:

infinity.loadModule('infinity.process');
let node = infinity.process.which('node');

if (node !== '') {
    let result = infinity.process.start(node, '--version', true);
    console.info(result.output);
}