Class OsProcess


  • public class OsProcess
    extends java.lang.Object
    A 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
      long exitCode()
      Get the exit code of the OS process - Applicable only for OS processes that are not started as daemon.
      java.lang.String getDaemonOutputFilePath()
      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.
      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.
      long pid()
      Get the process PID.
      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.
      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.
      void setDaemon​(boolean isDaemon)
      Set whether the OS process is executed as a daemon (default: false).
      void setenv​(java.lang.String varName, java.lang.String value)
      Set an environment variable.
      boolean start()
      Start the OS process.
      void unsetenv​(java.lang.String varName)
      Remove an environment variable.
      void waitFor()
      Wait for the termination of the OS process - Applicable only for OS processes that are not started as daemon.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • setenv

        public void setenv​(java.lang.String varName,
                           java.lang.String value)
        Set an environment variable.
        Parameters:
        varName - the variable name
        value - 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.IOException
        Set 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.IOException
        Get 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.InterruptedException
        Wait 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()