ProcessBuilder java.io.IOException: Cannot run program error=2, No such file or directory

在通过java kill 进程(Spark StructredStreaming  driver端残留的进程)时

通过java ProcessBuilder执行如下命令,

        String killCommand = String.format("ps -ef|grep java|grep 'kafka_sink_partitition_test_2019_0730/conf/'|awk '{print $2}'|xargs kill -9",taskName);
        try {
            LOG.info(String.format("starting to execute kill command: %s",killCommand));
            ProcessBuilder pb = new ProcessBuilder(killCommand);
            Process process = pb.start();
            int errCode = process.waitFor();
            LOG.info(String.format("finished kill local driver task: %s  code: %s",taskName,errCode ));
        } catch (Exception e) {
            LOG.warn(String.format("kill local driver task: %s error: ",taskName),e);
        }

报错信息

java.io.IOException: Cannot run program "ps -ef|grep java|grep 'kafka_sink_partitition_test_2019_0730/conf/'|awk '{print $2}'|xargs kill -9": error=2, No such file or directory

这种方式执行pipe命令会有问题Using Java ProcessBuilder to Execute a Piped Command

返回code137 应该是128+9

应该改为:

 ProcessBuilder pb = new ProcessBuilder("/bin/sh", "-c",killCommand);

 

你可能感兴趣的:(ProcessBuilder java.io.IOException: Cannot run program error=2, No such file or directory)