JAVA_RUN run process for Matlab only
with optional cwd, env. vars, stdin, timeout
handles command lines with spaces input each segment of the command as an element in a string array this is how python subprocess.run works
Contents
Inputs
- cmd_array: vector of string to compose a command line
- opt.env: environment variable struct to set
- opt.cwd: working directory to use while running command
- opt.stdin: string to pass to subprocess stdin pipe - OK to use with timeout
- opt.timeout: time to wait for process to complete before erroring (seconds)
- opt.stdout: logical to indicate whether to use pipe for stdout
- opt.stderr: logical to indicate whether to use pipe for stderr
Outputs
- status: 0 is generally success. -1 if timeout. Other codes as per the program / command run
- stdout: stdout from process
- stderr: stderr from process
Example
subprocess_run(["mpiexec", "-help2"]) subprocess_run(["sh", "-c", "ls", "-l"]) subprocess_run(["cmd", "/c", "dir", "/Q", "/L"])
NOTE: if cwd option used, any paths must be absolute or relative to cwd. otherwise, they are relative to pwd.
uses Matlab Java ProcessBuilder interface to run subprocess and use stdin/stdout pipes https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/ProcessBuilder.html
process instantiation
Gfortran streams
https://www.mathworks.com/matlabcentral/answers/91919-why-does-the-output-of-my-fortran-script-not-show-up-in-the-matlab-command-window-when-i-execute-it#answer_101270 Matlab grabs the stdout, stderr, stdin handles of a Gfortran program, even when it's using Java. We must disable this behavior for the duration the running process.
start process
https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/ProcessBuilder.html#start()
stdin pipe
read stdout, stderr pipes
like Python subprocess.run, this may block or deadlock if the process writes large amounts of data to stdout or stderr. A better approach is to read each of the streams in a separate thread.
wait for process to complete
https://docs.oracle.com/en/java/javase/23/docs/api/java.base/java/lang/Process.html#waitFor()