infinity.process¶
Module: infinity.process
Provides functionality for starting external programs and for checking, waiting for and killing running processes.
Note:
exists(),kill()andwaitFor()are only implemented on Windows; on other platforms they always returnfalse. On Linux,start()only works withwaitfor = true(seestart()).
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);
}
result¶
An object according to this interface is being returned by the start function.
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).
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:
stringThe 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:
stringThe 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:
stringThe 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, seewhich();.cmdand.batfiles are run throughcmd.exe. The working directory is the folder offileNameif 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), soPATHis searched and shell syntax is possible. The process is only started ifwaitforistrue; withwaitfor = falsenothing is started andsuccessisfalse. Thetimeoutparameter is ignored, and the working directory is not changed.
Signature:
start( fileName: string, params?: string, waitfor?: boolean, timeout?: number ): infinity.process.result
Parameters:
-
fileName:
stringFilename, 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, optionalCommand line parameters which should be provided to the started process, appended to the quoted file name. Use
correctCmdLineParam()to quote single parameters. -
waitfor:
boolean, optionalSpecifies whether the function should wait for the launched process to complete, before returning its results (exit code and output). Default:
false. -
timeout:
number, optionalWindows only: maximum number of seconds to wait for the process if
waitforistrue. If the process is still running after this time, it is killed andsuccessisfalse.0or 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:
stringThe name of the process to wait for.
-
waitFor:
numberNumber of seconds to wait for the process to finish or terminate (attention: this parameter is a number of seconds, not milliseconds).
0waits 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:
stringThe 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);
}