List of usage examples for org.apache.commons.exec CommandLine addArgument
public CommandLine addArgument(final String argument, final boolean handleQuoting)
From source file:org.excalibur.service.aws.CommandExample.java
public static void main(String[] args) throws ExecuteException, IOException, InterruptedException { CommandLine command = new CommandLine("/bin/bash"); command.addArgument("-c", false); command.addArgument("iperf3 -t 30 -c iperf.scottlinux.com >> output.txt", false); //Process process = Runtime.getRuntime().exec(new String[]{"bash", "-c", "iperf3 -t 60 -c localhost"}); // System.out.println(new Mirror().on(process).get().field("pid")); //process.waitFor(); // System.out.println(process.exitValue()); // ManagementFactory.getRuntimeMXBean().getName(); // System.out.println(IOUtils.readLines(process.getInputStream())); //String command = "iperf3 -t 30 -c iperf.scottlinux.com"; ExecuteWatchdog watchdog = new ExecuteWatchdog(10); final DefaultExecutor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler()); executor.setExitValue(1);/*from w ww . j a v a2 s.com*/ executor.execute(command, new ExecuteResultHandler() { @Override public void onProcessFailed(ExecuteException e) { e.printStackTrace(); } @Override public void onProcessComplete(int exitValue) { System.out.println(exitValue); } }); }
From source file:org.excalibur.service.compute.executor.Worker.java
public void execute(final Application application, ExecuteResultHandler executeResultHandler) throws ExecuteException, IOException { final String commandLine = application.getExecutableCommandLine(); DefaultExecutor executor = new DefaultExecutor(); CommandLine command = new CommandLine("/bin/sh"); command.addArgument("-c", false); command.addArgument(commandLine, false); executor.execute(command, System.getenv(), executeResultHandler); LOG.debug("Launched the execution of task: [{}], uuid: [{}]", commandLine, application.getId()); }
From source file:org.geoserver.importer.transform.AbstractCommandLinePreTransform.java
protected void setupCommandLine(boolean inline, CommandLine cmd) { for (String option : options) { cmd.addArgument(option, false); }/*from w w w .ja v a2 s . c o m*/ // setup input and output files if (inline) { cmd.addArgument("${input}", false); } else { if (isOutputAfterInput()) { cmd.addArgument("${input}", false); cmd.addArgument("${output}", false); } else { cmd.addArgument("${output}", false); cmd.addArgument("${input}", false); } } }
From source file:org.geoserver.importer.transform.PostScriptTransform.java
@Override public void apply(ImportTask task, ImportData data) throws Exception { File executable = getExecutable(); CommandLine cmd = new CommandLine(executable); for (String option : options) { cmd.addArgument(option, false); }//from ww w . ja v a 2 s .com execute(cmd, getScriptsFolder().dir()); }
From source file:org.kitodo.imagemanagement.ConvertRunner.java
/** * Executes the ImageMagick command using Apache Commons Exec. * * @param commandLine//ww w.jav a2 s . co m * command line to execute * @throws IOException * if I/O fails */ void run(IMOperation commandLine) throws IOException { Executor executor = new DefaultExecutor(); OutputStream outAndErr = new ByteArrayOutputStream(); executor.setStreamHandler(new PumpStreamHandler(outAndErr)); long timeoutMillis = 1000 * KitodoConfig.getIntParameter(ParameterImageManagement.TIMEOUT_SEC, DEFAULT_TIMEOUT_MINS); executor.setWatchdog(new ExecuteWatchdog(timeoutMillis)); CommandLine command; try { String sshHosts = KitodoConfig.getParameter(ParameterImageManagement.SSH_HOST); command = new CommandLine("ssh"); String[] hosts = sshHosts.split(","); String host = hosts[RANDOMNESS_GENERATOR.nextInt(hosts.length)]; command.addArgument(host, false); command.addArgument(convertCommand + ' ' + commandLine.toString(), false); } catch (NoSuchElementException e) { logger.trace("SSH not configured.", e); command = new CommandLine(convertCommand); command.addArguments(commandLine.toString()); } try { logger.debug("Executing: {}", command); logger.trace("Timeout: {} mins", timeoutMillis / 60000d); executor.execute(command); logger.debug("Command output:{}{}", System.lineSeparator(), outAndErr.toString()); } catch (IOException | RuntimeException e) { logger.error("Command output:{}{}", System.lineSeparator(), outAndErr.toString()); throw e; } }
From source file:org.kitodo.imagemanagementmodule.ConvertRunner.java
/** * Executes the ImageMagick command using Apache Commons Exec. * * @param commandLine//from w w w . j a v a2 s . c om * command line to execute * @throws IOException * if I/O fails */ void run(IMOperation commandLine) throws IOException { Executor executor = new DefaultExecutor(); OutputStream outAndErr = new ByteArrayOutputStream(); executor.setStreamHandler(new PumpStreamHandler(outAndErr)); long timeoutMillis = 1000 * Config.getIntParameter("ImageManagementModule.timeoutSec", DEFAULT_TIMEOUT_MINS); executor.setWatchdog(new ExecuteWatchdog(timeoutMillis)); CommandLine command; try { String sshHosts = Config.getParameter("ImageManagementModule.sshHosts"); command = new CommandLine("ssh"); String[] hosts = sshHosts.split(","); String host = hosts[RANDOMNESS_GENERATOR.nextInt(hosts.length)]; command.addArgument(host, false); command.addArgument(convertCommand + ' ' + commandLine.toString(), false); } catch (NoSuchElementException e) { logger.trace("SSH not configured.", e); command = new CommandLine(convertCommand); command.addArguments(commandLine.toString()); } try { logger.debug("Executing: {}", command); logger.trace("Timeout: {} mins", timeoutMillis / 60000d); executor.execute(command); logger.debug("Command output:{}{}", System.lineSeparator(), outAndErr.toString()); } catch (IOException | RuntimeException e) { logger.error("Command output:{}{}", System.lineSeparator(), outAndErr.toString()); throw e; } }
From source file:org.lucidj.launcher.Launcher.java
private void addArgument(CommandLine cmdline, String name, String value) { cmdline.addArgument("-D" + name + "=" + value, false); }
From source file:org.mail.bridge.FolderMonitor.java
public synchronized void runScriptAgainstReceivedFiles(List<File> inboxFiles) { if (config.getInboxScript().isEmpty() || Utils.isEmpty(inboxFiles)) return;/*www .j av a2 s .c o m*/ LOG.debug("Run script '{}' against files {}", config.getInboxScript(), inboxFiles); ByteArrayOutputStream out = new ByteArrayOutputStream(); try { CommandLine cmd = CommandLine.parse(config.getInboxScript()); for (File file : inboxFiles) cmd.addArgument(file.getName(), true); DefaultExecutor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler(out)); executor.setWatchdog(new ExecuteWatchdog(SCRIPT_TIMEOUT)); Map<String, String> environment = EnvironmentUtils.getProcEnvironment(); environment.putAll(config.asEnvironmentMap()); executor.setWorkingDirectory(new File(System.getProperty("user.dir"))); executor.execute(cmd, environment); LOG.info("Script '{}' successfully finished", config.getInboxScript()); LOG.debug("Script output:\n{}", out.toString()); } catch (ExecuteException e) { LOG.error(e.getMessage(), e); LOG.error("\nScript '{}' output:\n{}", config.getInboxScript(), out.toString()); int c = config.getInboxScriptStopCode(); if (c != 0 && c == e.getExitValue()) postMessage(new Main.StopMessage( String.format("Script '%s' exited with code %d that is configured as stop code", config.getInboxScript(), c))); } catch (IOException e) { LOG.error(e.getMessage(), e); LOG.error("\nScript '{}' output:\n{}", config.getInboxScript(), out.toString()); } }
From source file:org.n52.movingcode.runtime.processors.python.PythonCLIProcessor.java
/** * Creates a CommandLine Object for execution * /*www. ja va2s .co m*/ * @param paramSet * - the parameter specification * @param executionValues * - the values for the parameters * @return CommandLine - an executable CommandLine */ private static CommandLine buildCommandLine(String executable, SortedMap<ParameterID, String[]> executionValues, SortedMap<ParameterID, IOParameter> paramMap) throws IllegalArgumentException { CommandLine commandLine = CommandLine.parse(executable); // assemble commandLine with values, separators and all that stuff for (ParameterID identifier : executionValues.keySet()) { String argument = ""; // 1. add prefix argument = argument + paramMap.get(identifier).printPrefix(); // 2. add values with subsequent separator for (String value : executionValues.get(identifier)) { argument = argument + value + paramMap.get(identifier).printSeparator(); } // remove last occurrence of separator if (argument.length() != 0) { argument = argument.substring(0, argument.length() - paramMap.get(identifier).printSeparator().length()); } // 3. add suffix argument = argument + paramMap.get(identifier).printSuffix(); // 4. add to argument to CommandLine commandLine.addArgument(argument, false); } return commandLine; }
From source file:org.openhab.binding.exec.internal.ExecBinding.java
/** * <p>Executes <code>commandLine</code>. Sometimes (especially observed on * MacOS) the commandLine isn't executed properly. In that cases another * exec-method is to be used. To accomplish this please use the special * delimiter '<code>@@</code>'. If <code>commandLine</code> contains this * delimiter it is split into a String[] array and the special exec-method * is used.</p>//from w ww. j av a 2 s . c om * <p>A possible {@link IOException} gets logged but no further processing is * done.</p> * * @param commandLine the command line to execute * @return response data from executed command line */ private String executeCommandAndWaitResponse(String commandLine) { String retval = null; CommandLine cmdLine = null; if (commandLine.contains(CMD_LINE_DELIMITER)) { String[] cmdArray = commandLine.split(CMD_LINE_DELIMITER); cmdLine = new CommandLine(cmdArray[0]); for (int i = 1; i < cmdArray.length; i++) { cmdLine.addArgument(cmdArray[i], false); } } else { cmdLine = CommandLine.parse(commandLine); } DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout); Executor executor = new DefaultExecutor(); ByteArrayOutputStream stdout = new ByteArrayOutputStream(); PumpStreamHandler streamHandler = new PumpStreamHandler(stdout); executor.setExitValue(1); executor.setStreamHandler(streamHandler); executor.setWatchdog(watchdog); try { executor.execute(cmdLine, resultHandler); logger.debug("executed commandLine '{}'", commandLine); } catch (ExecuteException e) { logger.error("couldn't execute commandLine '" + commandLine + "'", e); } catch (IOException e) { logger.error("couldn't execute commandLine '" + commandLine + "'", e); } // some time later the result handler callback was invoked so we // can safely request the exit code try { resultHandler.waitFor(); int exitCode = resultHandler.getExitValue(); retval = StringUtils.chomp(stdout.toString()); logger.debug("exit code '{}', result '{}'", exitCode, retval); } catch (InterruptedException e) { logger.error("Timeout occured when executing commandLine '" + commandLine + "'", e); } return retval; }