Search code examples
javasysctlapache-commons-exec

Error while running command consist of multiple parameters using apache commons exec


I am having trouble while running following command using apache commons exec: sysctl -n net.ipv4.neigh.default.gc_thresh3

The problem is it is giving the following error:

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

Below is my code:

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);
        }
}

I am using the cmd - /bin/sh and params - -c sysctl -n net.ipv4.neigh.default.gc_thresh3.

How can i run commands like that without facing such kind of error using apache commons exec?
I dont need the complete solution. Any good direction is fine with me.


Solution

  • The problem is /bin/sh is not the proper path to run this kind of command. The command should be executed in: /sbin/sysctl.
    Also, different operating system requires different path to run. see man sysctl
    So, the cmd is - /sbin/sysctl and params - new String[] { "-n", "net.ipv4.neigh.default.gc_thresh3" }
    The code will look like this:

    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);
            }
    }