Class OsProcess
- java.lang.Object
-
- com.dkfqs.measuringagent.javascript.executor.OsProcess
-
public class OsProcess extends java.lang.ObjectA OS process executed by JavaScript.
JavaScript Example:const osProcess = OsProcess.newInstance(['cmd.exe', '/C', 'dir']); // const osProcess = OsProcess.newInstance(['bash', '-c', 'ls -al']); // osProcess.setDaemon(true); // osProcess.setenv('MyName', 'David'); // osProcess.unsetenv('LS_COLORS'); osProcess.setCollectStdout(); osProcess.setCollectStderr(); osProcess.start(); //console.log(`pid = ${osProcess.pid()}`); osProcess.waitFor(); //console.log(`exitCode = ${osProcess.exitCode()}`); osProcess.getStdoutLines().forEach(line => { console.log(`stdout : ${line}`) }) osProcess.getStderrLines().forEach(line => { console.log(`stderr : ${line}`) })
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description longexitCode()Get the exit code of the OS process - Applicable only for OS processes that are not started as daemon.java.lang.StringgetDaemonOutputFilePath()Get the OS process daemon output file path.java.lang.String[]getStderrLines()Get the stderr output of the OS process after its termination - Applicable only for OS processes that are not started as daemon.java.lang.String[]getStdoutLines()Get the stdout output of the OS process after its termination - Applicable only for OS processes that are not started as daemon.booleanisDaemon()Get if this OS process is a daemon, which means that it will not be terminated after the end of the script execution.longpid()Get the process PID.voidsetCollectStderr()Collect the stderr output of the OS process instead writing it to the console log - Applicable only for OS processes that are not started as daemon.voidsetCollectStdout()Collect the stdout output of the OS process instead writing it to the console log - Applicable only for OS processes that are not started as daemon.voidsetDaemon(boolean isDaemon)Set whether the OS process is executed as a daemon (default: false).voidsetenv(java.lang.String varName, java.lang.String value)Set an environment variable.booleanstart()Start the OS process.voidunsetenv(java.lang.String varName)Remove an environment variable.voidwaitFor()Wait for the termination of the OS process - Applicable only for OS processes that are not started as daemon.
-
-
-
Method Detail
-
setenv
public void setenv(java.lang.String varName, java.lang.String value)Set an environment variable.- Parameters:
varName- the variable namevalue- the value
-
unsetenv
public void unsetenv(java.lang.String varName)
Remove an environment variable.- Parameters:
varName- the variable name
-
setDaemon
public void setDaemon(boolean isDaemon) throws java.io.IOExceptionSet whether the OS process is executed as a daemon (default: false).- Parameters:
isDaemon- if true then set the OS process as daemon, which means that it will not be terminated after the end of the script execution- Throws:
java.io.IOException- if the OS process is set as daemon and the log file of the daemon cannot be created- See Also:
JavaScriptRhinoTestJobExecutorContext.isJavaScriptStartOsProcessesAsDaemonEnabled()
-
isDaemon
public boolean isDaemon()
Get if this OS process is a daemon, which means that it will not be terminated after the end of the script execution.- Returns:
- true if this OS process is a daemon
-
getDaemonOutputFilePath
public java.lang.String getDaemonOutputFilePath() throws java.io.IOExceptionGet the OS process daemon output file path.- Returns:
- the OS process daemon output file path
- Throws:
java.io.IOException- if an I/O error occurs
-
setCollectStdout
public void setCollectStdout()
Collect the stdout output of the OS process instead writing it to the console log - Applicable only for OS processes that are not started as daemon.
-
setCollectStderr
public void setCollectStderr()
Collect the stderr output of the OS process instead writing it to the console log - Applicable only for OS processes that are not started as daemon.
-
start
public boolean start()
Start the OS process.- Returns:
- true if the OS process was started, or false if the start of the OS process failed.
-
pid
public long pid()
Get the process PID.- Returns:
- the process PID, or -1 if the process was not started
-
waitFor
public void waitFor() throws java.lang.InterruptedExceptionWait for the termination of the OS process - Applicable only for OS processes that are not started as daemon.- Throws:
java.lang.InterruptedException- th the current thread is interrupted
-
exitCode
public long exitCode()
Get the exit code of the OS process - Applicable only for OS processes that are not started as daemon.- Returns:
- the exit code of the OS process, or -1 if the process was not started or is still running
-
getStdoutLines
public java.lang.String[] getStdoutLines()
Get the stdout output of the OS process after its termination - Applicable only for OS processes that are not started as daemon.- Returns:
- an array lines
- See Also:
setCollectStdout(),waitFor()
-
getStderrLines
public java.lang.String[] getStderrLines()
Get the stderr output of the OS process after its termination - Applicable only for OS processes that are not started as daemon.- Returns:
- an array lines
- See Also:
setCollectStdout(),waitFor()
-
-