Example usage for org.apache.commons.exec CommandLine CommandLine

List of usage examples for org.apache.commons.exec CommandLine CommandLine

Introduction

In this page you can find the example usage for org.apache.commons.exec CommandLine CommandLine.

Prototype

public CommandLine(final CommandLine other) 

Source Link

Document

Copy constructor.

Usage

From source file:de.fischer.thotti.core.runner.NDRunner.java

private NDTestResult forkNDTestRunnerInSlaveMode(NonDistributedTestType test, TestInstanceType instance)
        throws IOException {
    NDTestResult result = new NDTestResult();
    GregorianCalendar startTime;//from  w ww . j a  va 2  s. co m
    long startTimeMS;
    long endTimeMS;
    int exitCode = -9999;

    result.setTestId(test.getId());
    result.setJVMArgs(instance.getJvmArguments());
    result.setJVMArgsID(instance.getJvmArgsID());
    result.setParamGrpID(instance.getName());

    CommandLine cmdLine = new CommandLine("java");

    if (false == StringUtils.isEmpty(instance.getJvmArguments())) {
        cmdLine.addArguments(instance.getJvmArguments());
    }

    cmdLine.addArgument("-cp").addArgument(System.getProperty("java.class.path"))
            .addArgument(NDRunner.class.getName()).addArgument("--slave").addArgument("--executionid")
            .addArgument(instance.getExecutionID().toString());

    System.gc();
    DefaultExecutor executor = new DefaultExecutor();

    startTime = (GregorianCalendar) Calendar.getInstance();
    startTimeMS = startTime.getTimeInMillis();

    try {
        exitCode = executor.execute(cmdLine);
        result.setSuccessfulTermination(true);
        result.setExitCode(exitCode);
    } catch (ExecuteException e) {
        result.setSuccessfulTermination(false);
        result.setExitCode(e.getExitValue());
    } finally {
        endTimeMS = System.currentTimeMillis();
    }

    result.setExecutionTime(endTimeMS - startTimeMS);
    result.setStartTime(startTime);

    System.out.println(result);

    return result;
}

From source file:com.netflix.genie.web.tasks.job.JobCompletionService.java

/**
 * An external fail-safe mechanism to clean up processes left behind by the run.sh after the
 * job is killed or failed. This method is a no-op for jobs whose status is INVALID.
 *
 * @param jobId The id of the job to cleanup processes for.
 *///from   w  w w.  j  av  a 2s.  c om
private void cleanupProcesses(final String jobId) {
    try {
        if (!this.jobSearchService.getJobStatus(jobId).equals(JobStatus.INVALID)) {
            this.jobSearchService.getJobExecution(jobId).getProcessId().ifPresent(pid -> {
                try {
                    final CommandLine commandLine = new CommandLine(JobConstants.UNIX_PKILL_COMMAND);
                    commandLine.addArgument(JobConstants.getKillFlag());
                    commandLine.addArgument(Integer.toString(pid));
                    this.executor.execute(commandLine);

                    // The process group should not exist and the above code should always throw and exception.
                    // If it does not then the bash script is not cleaning up stuff well during kills
                    // or the script is done but child processes are still remaining. This metric tracks all that.
                    this.processGroupCleanupFailureRate.increment();
                } catch (final Exception e) {
                    log.debug("Received expected exception. Ignoring.");
                }
            });
        }
    } catch (final GenieException ge) {
        log.error("Unable to cleanup process for job due to exception. " + jobId, ge);
        this.processGroupCleanupFailureRate.increment();
    }
}

From source file:com.tupilabs.pbs.PBS.java

/**
 * <p>//from   w ww . j av  a 2s  .  co  m
 * PBS qsub command with arguments resource overrides
 * </p>
 *
 * <p>
 * Equivalent to qsub [param] -l [resource_name=value,resource_name=value]]
 * </p>
 *
 * @param input job input file
 * @param resourceOverrides variable number of resources to override
 * @return job id
 */
public static String qsub(String input, String... resourceOverrides) {
    final CommandLine cmdLine = new CommandLine(COMMAND_QSUB);
    cmdLine.addArgument(PARAMETER_RESOURCE_OVERRIDE_STATUS);
    String resourceOverrideArgument = StringUtils.join(resourceOverrides, ",");
    cmdLine.addArgument(resourceOverrideArgument);
    cmdLine.addArgument(input);

    final OutputStream out = new ByteArrayOutputStream();
    final OutputStream err = new ByteArrayOutputStream();

    DefaultExecuteResultHandler resultHandler;
    try {
        resultHandler = execute(cmdLine, out, err);
        resultHandler.waitFor(DEFAULT_TIMEOUT);
    } catch (ExecuteException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    } catch (InterruptedException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    }

    final int exitValue = resultHandler.getExitValue();
    LOGGER.info("qsub exit value: " + exitValue);
    LOGGER.fine("qsub output: " + out.toString());

    if (exitValue != 0)
        throw new PBSException("Failed to submit job script " + input + ". Error output: " + err.toString());

    String jobId = out.toString();
    return jobId.trim();
}

From source file:info.pancancer.arch3.worker.WorkerRunnable.java

@Override
public void run() {

    int max = maxRuns;

    try {/*w ww .  j  av  a 2 s  .  c  o m*/
        // the VM UUID
        log.info(" WORKER VM UUID provided as: '" + vmUuid + "'");
        // write to
        // TODO: Add some sort of "local debug" mode so that developers working on their local
        // workstation can declare the queue if it doesn't exist. Normally, the results queue is
        // created by the Coordinator.
        resultsChannel = Utilities.setupExchange(settings, this.resultsQueueName);

        while (max > 0 || this.endless) {
            log.debug(max + " remaining jobs will be executed");
            log.info(" WORKER IS PREPARING TO PULL JOB FROM QUEUE " + this.jobQueueName);

            if (!endless) {
                max--;
            }

            // jobChannel needs to be created inside the loop because it is closed inside the loop, and it is closed inside this loop to
            // prevent pre-fetching.
            Channel jobChannel = Utilities.setupQueue(settings, this.jobQueueName);
            if (jobChannel == null) {
                throw new NullPointerException("jobChannel is null for queue: " + this.jobQueueName
                        + ". Something bad must have happened while trying to set up the queue connections. Please ensure that your configuration is correct.");
            }
            QueueingConsumer consumer = new QueueingConsumer(jobChannel);
            jobChannel.basicConsume(this.jobQueueName, false, consumer);

            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            log.info(vmUuid + "  received " + delivery.getEnvelope().toString());
            if (delivery.getBody() != null) {
                String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
                if (message.trim().length() > 0) {

                    log.info(" [x] Received JOBS REQUEST '" + message + "' @ " + vmUuid);

                    Job job = new Job().fromJSON(message);

                    Status status = new Status(vmUuid, job.getUuid(), StatusState.RUNNING,
                            Utilities.JOB_MESSAGE_TYPE, "job is starting", this.networkAddress);
                    status.setStderr("");
                    status.setStdout("");
                    String statusJSON = status.toJSON();

                    log.info(" WORKER LAUNCHING JOB");

                    // greedy acknowledge, it will be easier to deal with lost jobs than zombie workers in hostile OpenStack
                    // environments
                    log.info(vmUuid + " acknowledges " + delivery.getEnvelope().toString());
                    jobChannel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
                    // we need to close the channel IMMEDIATELY to complete the ACK.
                    jobChannel.close();
                    // Close the connection object as well, or the main thread may not exit because of still-open-and-in-use resources.
                    jobChannel.getConnection().close();

                    WorkflowResult workflowResult = new WorkflowResult();
                    if (testMode) {
                        workflowResult.setWorkflowStdout("everything is awesome");
                        workflowResult.setExitCode(0);
                    } else {
                        String seqwareEngine = settings.getString(Constants.WORKER_SEQWARE_ENGINE,
                                Constants.SEQWARE_WHITESTAR_ENGINE);
                        String seqwareSettingsFile = settings.getString(Constants.WORKER_SEQWARE_SETTINGS_FILE);
                        String dockerImage = settings.getString(Constants.WORKER_SEQWARE_DOCKER_IMAGE_NAME);
                        workflowResult = launchJob(statusJSON, job, seqwareEngine, seqwareSettingsFile,
                                dockerImage);
                    }

                    status = new Status(vmUuid, job.getUuid(),
                            workflowResult.getExitCode() == 0 ? StatusState.SUCCESS : StatusState.FAILED,
                            Utilities.JOB_MESSAGE_TYPE, "job is finished", networkAddress);
                    status.setStderr(workflowResult.getWorkflowStdErr());
                    status.setStdout(workflowResult.getWorkflowStdout());
                    statusJSON = status.toJSON();

                    log.info(" WORKER FINISHING JOB");

                    finishJob(statusJSON);
                } else {
                    log.info(NO_MESSAGE_FROM_QUEUE_MESSAGE);
                }
                // we need to close the channel *conditionally*
                if (jobChannel.isOpen()) {
                    jobChannel.close();
                }
                // Close the connection object as well, or the main thread may not exit because of still-open-and-in-use resources.
                if (jobChannel.getConnection().isOpen()) {
                    jobChannel.getConnection().close();
                }
            } else {
                log.info(NO_MESSAGE_FROM_QUEUE_MESSAGE);
            }

            if (endless) {
                log.info("attempting to reset workspace");
                DefaultExecutor executor = new DefaultExecutor();
                DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
                // attempt a cleanup
                CommandLine cli = new CommandLine("sudo");
                List<String> args = new ArrayList<>(Arrays.asList("rm", "-rf", "/datastore/*"));
                cli.addArguments(args.toArray(new String[args.size()]));
                executor.execute(cli, resultHandler);
                // Use the result handler for non-blocking call, so this way we should be able to get updates of
                // stdout and stderr while the command is running.
                resultHandler.waitFor();
                log.info("exit code for cleanup: " + resultHandler.getExitValue());
            }
        }
        log.info(" \n\n\nWORKER FOR VM UUID HAS FINISHED!!!: '" + vmUuid + "'\n\n");

        // turns out this is needed when multiple threads are reading from the same
        // queue otherwise you end up with multiple unacknowledged messages being undeliverable to other workers!!!
        if (resultsChannel != null && resultsChannel.isOpen()) {
            resultsChannel.close();
            resultsChannel.getConnection().close();
        }
        log.debug("result channel open: " + resultsChannel.isOpen());
        log.debug("result channel connection open: " + resultsChannel.getConnection().isOpen());
    } catch (Exception ex) {
        log.error(ex.getMessage(), ex);
    }
}

From source file:io.selendroid.android.impl.DefaultAndroidEmulator.java

public void unlockEmulatorScreen() throws AndroidDeviceException {
    CommandLine event82 = new CommandLine(AndroidSdk.adb());

    if (isSerialConfigured()) {
        event82.addArgument("-s", false);
        event82.addArgument(serial, false);
    }/*from w  w  w .  j a v  a2 s  .  co m*/
    event82.addArgument("shell", false);
    event82.addArgument("input", false);
    event82.addArgument("keyevent", false);
    event82.addArgument("82", false);

    try {
        ShellCommand.exec(event82, 20000);
    } catch (ShellCommandException e) {
        throw new AndroidDeviceException(e);
    }

    CommandLine event4 = new CommandLine(AndroidSdk.adb());

    if (isSerialConfigured()) {
        event4.addArgument("-s", false);
        event4.addArgument(serial, false);
    }
    event4.addArgument("shell", false);
    event4.addArgument("input", false);
    event4.addArgument("keyevent", false);
    event4.addArgument("4", false);
    try {
        ShellCommand.exec(event4, 20000);
    } catch (ShellCommandException e) {
        throw new AndroidDeviceException(e);
    }
}

From source file:ddf.content.plugin.video.VideoThumbnailPlugin.java

private CommandLine getFFmpegCreateThumbnailCommand(final String videoFilePath, final String thumbnailFilePath,
        final String seek, final int numFrames) {
    final String filterChainFlag = "-vf";
    final String filterChain = "thumbnail,scale=200:-1";
    final String videoFramesToOutputFlag = "-frames:v";
    final String videoFramesToOutput = String.valueOf(numFrames);
    final String videoSyncFlag = "-vsync";
    final String videoSyncVariableFrameRate = "vfr";

    final CommandLine command = new CommandLine(ffmpegPath).addArgument(SUPPRESS_PRINTING_BANNER_FLAG);

    if (seek != null) {
        final String seekFlag = "-ss";
        command.addArgument(seekFlag).addArgument(seek);
    }//w  w w .  ja  va  2s  . c o m

    command.addArgument(INPUT_FILE_FLAG).addArgument(videoFilePath, DONT_HANDLE_QUOTING)
            .addArgument(filterChainFlag).addArgument(filterChain).addArgument(videoFramesToOutputFlag)
            .addArgument(videoFramesToOutput)
            // The "-vsync vfr" argument prevents frames from being duplicated, which allows us
            // to get a different thumbnail for each of the output images.
            .addArgument(videoSyncFlag).addArgument(videoSyncVariableFrameRate);

    command.addArgument(thumbnailFilePath, DONT_HANDLE_QUOTING).addArgument(OVERWRITE_EXISTING_FILE_FLAG);

    return command;
}

From source file:com.tupilabs.pbs.PBS.java

/**
 * PBS qsub command for an Array Job with Specific PBS_ARRAY_IDs to submit
 * <p>//from ww  w  .j  a v  a  2s  . c  om
 * Equivalent to qsub -t 1,2,3 [param]
 *
 * @param input job input file
 * @param pbsArrayIDs list of specified PBS indices
 * @return job id of array job
 */
public static String qsubArrayJob(String input, List<Integer> pbsArrayIDs) {
    final CommandLine cmdLine = new CommandLine(COMMAND_QSUB);
    cmdLine.addArgument(PARAMETER_ARRAY_JOB_STATUS);
    String listArgument = StringUtils.join(pbsArrayIDs, ",");
    cmdLine.addArgument(listArgument);
    cmdLine.addArgument(input);

    final OutputStream out = new ByteArrayOutputStream();
    final OutputStream err = new ByteArrayOutputStream();

    DefaultExecuteResultHandler resultHandler;
    try {
        resultHandler = execute(cmdLine, out, err);
        resultHandler.waitFor(DEFAULT_TIMEOUT);
    } catch (ExecuteException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    } catch (InterruptedException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    }

    final int exitValue = resultHandler.getExitValue();
    LOGGER.info("qsub exit value: " + exitValue);
    LOGGER.fine("qsub output: " + out.toString());

    if (exitValue != 0)
        throw new PBSException("Failed to submit job script " + input + ". Error output: " + err.toString());

    String jobId = out.toString();
    return jobId.trim();
}

From source file:com.boundlessgeo.wps.grass.GrassProcesses.java

/**
 * Define a GISBASE/LOCATION_NAME/PERMANENT for the provided dem.
 *
 * The dem is staged in GISBASE/dem.tif and then moved to
 * GISBASE/LOCATION_NAME/PERMANENT/dem.tif
 *
 * @param operation/*from   w  w  w  .j a v  a  2  s.  c  o  m*/
 *            Name used for the location on disk
 * @param dem
 *            File used to establish CRS and Bounds for the location
 * @return Array of files consisting of {GISBASE, LOCATION, MAPSET, dem.tif}
 * @throws Exception
 */
static File location(File location, File raster) throws Exception {
    // grass70 + ' -c ' + myfile + ' -e ' + location_path
    CommandLine cmd = new CommandLine(EXEC);
    cmd.addArgument("-c");
    cmd.addArgument("${raster}");
    cmd.addArgument("-e");
    cmd.addArgument("${location}");
    cmd.setSubstitutionMap(new KVP("raster", raster, "location", location));
    LOGGER.info(cmd.toString());

    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(0);
    ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
    executor.setWatchdog(watchdog);
    executor.setStreamHandler(new PumpStreamHandler(System.out));

    LOGGER.info(cmd.toString());
    try {
        int exitValue = executor.execute(cmd);
    } catch (ExecuteException fail) {
        LOGGER.warning("grass70:" + fail.getLocalizedMessage());
        throw fail;
    }

    File mapset = new File(location, "PERMANENT");
    if (!mapset.exists()) {
        throw new IllegalStateException("Did not create mapset " + mapset);
    }
    return location;
}

From source file:io.selendroid.android.impl.DefaultAndroidEmulator.java

private void waitForLauncherToComplete(Boolean delay) throws AndroidDeviceException {
    CommandLine event = new CommandLine(AndroidSdk.adb());

    if (isSerialConfigured()) {
        event.addArgument("-s", false);
        event.addArgument(serial, false);
    }//from   www  . j  a  v  a  2s  .c  o m
    event.addArgument("shell", false);
    event.addArgument("ps", false);
    String homeScreenLaunched = null;
    try {
        homeScreenLaunched = ShellCommand.exec(event, 20000);
    } catch (ShellCommandException e) {
        throw new AndroidDeviceException(e);
    }
    if (homeScreenLaunched != null && homeScreenLaunched.contains("S com.android.launcher")) {
        if (!delay)
            return;
    } else {
        // it's still running, sleep for a bit
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        waitForLauncherToComplete(true);
    }

    // it's done right? ... well, maybe... check again after waiting a second
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
    waitForLauncherToComplete(false);

}

From source file:com.theoryinpractise.clojure.AbstractClojureCompilerMojo.java

protected void callClojureWith(ExecutionMode executionMode, File[] sourceDirectory, File outputDirectory,
        List<String> compileClasspathElements, String mainClass, String[] clojureArgs)
        throws MojoExecutionException {

    outputDirectory.mkdirs();//from   www  .  ja va 2s  .  co  m

    String classpath = manifestClasspath(sourceDirectory, outputDirectory, compileClasspathElements);

    final String javaExecutable = getJavaExecutable();
    getLog().debug("Java exectuable used:  " + javaExecutable);
    getLog().debug("Clojure manifest classpath: " + classpath);
    CommandLine cl = null;

    if (ExecutionMode.INTERACTIVE == executionMode && SystemUtils.IS_OS_WINDOWS
            && spawnInteractiveConsoleOnWindows) {
        Scanner sc = new Scanner(windowsConsole);
        Pattern pattern = Pattern.compile("\"[^\"]*\"|'[^']*'|[\\w'/]+");
        cl = new CommandLine(sc.findInLine(pattern));
        String param;
        while ((param = sc.findInLine(pattern)) != null) {
            cl.addArgument(param);
        }
        cl.addArgument(javaExecutable);
    } else {
        cl = new CommandLine(javaExecutable);
    }

    if (vmargs != null) {
        cl.addArguments(vmargs, false);
    }

    cl.addArgument("-Dclojure.compile.path=" + escapeFilePath(outputDirectory), false);

    if (warnOnReflection)
        cl.addArgument("-Dclojure.compile.warn-on-reflection=true");

    cl.addArguments(clojureOptions, false);

    cl.addArgument("-jar");
    File jar;
    if (prependClasses != null && prependClasses.size() > 0) {
        jar = createJar(classpath, prependClasses.get(0));
        cl.addArgument(jar.getAbsolutePath(), false);
        List<String> allButFirst = prependClasses.subList(1, prependClasses.size());
        cl.addArguments(allButFirst.toArray(new String[allButFirst.size()]));
        cl.addArgument(mainClass);
    } else {
        jar = createJar(classpath, mainClass);
        cl.addArgument(jar.getAbsolutePath(), false);
    }

    if (clojureArgs != null) {
        cl.addArguments(clojureArgs, false);
    }

    getLog().debug("Command line: " + cl.toString());

    Executor exec = new DefaultExecutor();
    Map<String, String> env = new HashMap<String, String>(System.getenv());
    //        env.put("path", ";");
    //        env.put("path", System.getProperty("java.home"));

    ExecuteStreamHandler handler = new PumpStreamHandler(System.out, System.err, System.in);
    exec.setStreamHandler(handler);
    exec.setWorkingDirectory(getWorkingDirectory());
    ShutdownHookProcessDestroyer destroyer = new ShutdownHookProcessDestroyer();
    exec.setProcessDestroyer(destroyer);

    int status;
    try {
        status = exec.execute(cl, env);
    } catch (ExecuteException e) {
        status = e.getExitValue();
    } catch (IOException e) {
        status = 1;
    }

    if (status != 0) {
        throw new MojoExecutionException("Clojure failed.");
    }

}