processbuilder和runtime.getruntime().exec(cmd)如何依赖于bash和sh命令?

jobtbby3  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(211)

所有这些行在mac中没有docker时运行良好,但在docker环境中失败。

try {
            log.info("FileSortHelper.sortResultFile start the file name is {}", resultFilePath);
            final String[] cmd = {
                FileServiceConstants.BASH, FileServiceConstants.SORTING_SCRIPT_PATH.toString(),
                resultFilePath };
            log.info("FileSortHelper.sortResultFile  Command run {}",
                cmd[0] + " " + cmd[1] + " " + cmd[2]);

            final long startTime = System.currentTimeMillis();
            final Process process = runtime.exec(cmd);
            process.waitFor();
            bufferedReader = new BufferedReader(
                    new InputStreamReader(process.getInputStream(), Charset.defaultCharset()));

            MDC.put(MdcConstants.PERF_MATRIX_TIME_TAKEN_FOR_SORTING_RESULT_FILE,
                String.valueOf(System.currentTimeMillis() - startTime));

            final StringBuilder responseFromScript = new StringBuilder();
            // Collect all the echo statement and print in logs
            while (bufferedReader.ready()) {
                responseFromScript.append(bufferedReader.readLine());
            }
            log.info("FileSortHelper.sortResultFile Response received from script: {}",
                responseFromScript);
            log.info("FileSortHelper.sortResultFile Exit code received from script: {}",
                process.exitValue());

        }

这个。下面是一个不同的组合,我测试。抱歉,如果这是一个非常基本的问题。

final String[] cmd ={"sh", SCRIPT_PATH.toString(), resultFilePath};
//Error cases received response : exit status 2
ProcessBuilder processBuilder = new ProcessBuilder();
        processBuilder.command("bash", "-c", DST_PATH.toString(), resultFilePath);

//Error cases received response : exit status 2
final Process process = Runtime.getRuntime().exec(cmd);

//Success cases received response : exit status o
processBuilder.command("bash", DST_PATH.toString(), resultFilePath);

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题