欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-05-12 15:01:26
...

通过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);