2

使用 apache commons exec 运行以下命令时遇到问题:sysctl -n net.ipv4.neigh.default.gc_thresh3

问题是它给出了以下错误:

Exception in thread "main" java.lang.RuntimeException: Could not run command [/bin/sh, -c, sysctl, -n, net.ipv4.neigh.default.gc_thresh3]
    at Testing.runCommand(Testing.java:44)
    at Testing.main(Testing.java:97)
Caused by: org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
    at org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:404)
    at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:166)
    at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:153)
    at Testing.runCommand(Testing.java:31)
    ... 1 more

下面是我的代码:

private String runCommand(String cmd, String params) {
        CommandLine commandLine = new CommandLine(cmd);
        commandLine.addArguments(params);
        ByteArrayOutputStream stdout = new ByteArrayOutputStream();
        ByteArrayOutputStream stderr = new ByteArrayOutputStream();
        PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(stdout, stderr);
        ExecuteWatchdog watchdog = new ExecuteWatchdog(30000);  // 30s timeout
        DefaultExecutor executor = new DefaultExecutor();
        executor.setStreamHandler(pumpStreamHandler);
        executor.setWatchdog(watchdog);

        try {
            int retCode = executor.execute(commandLine);
            System.out.println("Executed '" + cmd + "'\n"
                    + "returnCode: " + retCode + "\n"
                    + "stdout:\n" + stdout.toString() + "\n"
                    + "stderr:\n" + stderr.toString());

            if (retCode == 0) {
                return stdout.toString();
            } else {
                throw new NonZeroExitStatusReturnedException(commandLine.toString(), retCode);
            }

        } catch (IOException e) {
            throw new RuntimeException("Could not run command "+ commandLine.toString(), e);
        }
}

我正在使用 cmd -/bin/sh和 params - -c sysctl -n net.ipv4.neigh.default.gc_thresh3

如何使用 apache commons exec 运行这样的命令而不会遇到此类错误?
我不需要完整的解决方案。任何好的方向都适合我。

4

1 回答 1

0

问题/bin/sh不是运行这种命令的正确路径。该命令应在以下位置执行:/sbin/sysctl.
此外,不同的操作系统需要不同的运行路径。看到man sysctl
所以,cmd 是 -/sbin/sysctl和 params -new String[] { "-n", "net.ipv4.neigh.default.gc_thresh3" }
代码将如下所示:

private String runCommand(String cmd, String[] params) {
        CommandLine commandLine = new CommandLine(cmd);
        commandLine.addArguments(params, false);
        ByteArrayOutputStream stdout = new ByteArrayOutputStream();
        ByteArrayOutputStream stderr = new ByteArrayOutputStream();
        PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(stdout, stderr);
        ExecuteWatchdog watchdog = new ExecuteWatchdog(30000);  // 30s timeout
        DefaultExecutor executor = new DefaultExecutor();
        executor.setStreamHandler(pumpStreamHandler);
        executor.setWatchdog(watchdog);

        try {
            int retCode = executor.execute(commandLine);
            System.out.println("Executed '" + cmd + "'\n"
                    + "returnCode: " + retCode + "\n"
                    + "stdout:\n" + stdout.toString() + "\n"
                    + "stderr:\n" + stderr.toString());

            if (retCode == 0) {
                return stdout.toString();
            } else {
                throw new NonZeroExitStatusReturnedException(commandLine.toString(), retCode);
            }

        } catch (IOException e) {
            throw new RuntimeException("Could not run command "+ commandLine.toString(), e);
        }
}
于 2018-04-29T20:27:57.153 回答