Example usage for org.apache.commons.exec DefaultExecuteResultHandler waitFor

List of usage examples for org.apache.commons.exec DefaultExecuteResultHandler waitFor

Introduction

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

Prototype

public void waitFor(final long timeout) throws InterruptedException 

Source Link

Document

Causes the current thread to wait, if necessary, until the process has terminated.

Usage

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

/**
 * PBS qdel command.//from www . j av a2 s.com
 * <p>
 * Equivalent to qdel [param]
 *
 * @param jobId job id
 */
public static void qdel(String jobId) {
    final CommandLine cmdLine = new CommandLine(COMMAND_QDEL);
    cmdLine.addArgument(jobId);

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

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

    final int exitValue = resultHandler.getExitValue();
    LOGGER.info("qdel exit value: " + exitValue);

    if (exitValue != 0)
        throw new PBSException("Failed to delete job " + jobId + ". Error output: " + err.toString());
}

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

/**
 * <p>/*from  w w  w  . j a  v a2s . c o  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:com.tupilabs.pbs.PBS.java

/**
 * PBS qsub command for an Array Job with Specific PBS_ARRAY_IDs to submit
 * <p>/*from  w w  w  .  jav  a2s  .  co m*/
 * 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.tupilabs.pbs.PBS.java

/**
 * PBS qsub command for an Array Job with Specific PBS_ARRAY_IDs to submit
 * <p>//  w w  w . j  a  v  a  2 s.  c  o m
 * Equivalent to qsub -t 5-20 [param]
 *
 * @param input job input file
 * @param beginIndex beginning of index range
 * @param endIndex end of index range
 * @return job id of array job
 */
public static String qsubArrayJob(String input, int beginIndex, int endIndex) {
    final CommandLine cmdLine = new CommandLine(COMMAND_QSUB);
    cmdLine.addArgument(PARAMETER_ARRAY_JOB_STATUS);
    String rangeArgument = beginIndex + "-" + endIndex;
    cmdLine.addArgument(rangeArgument);
    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.tupilabs.pbs.PBS.java

/**
 * PBS qsub command for an Array Job with Specific PBS_ARRAY_IDs to submit AND a range to submit
 * <p>/*from www .  j av  a 2 s. com*/
 * Equivalent to qsub -t 1,2,3,5-20 [param]
 *
 * @param input job input file
 * @param pbsArrayIDs list of specified indices
 * @param beginIndex beginning of index range
 * @param endIndex end of index range
 * @return job id of array job
 */
public static String qsubArrayJob(String input, List<Integer> pbsArrayIDs, int beginIndex, int endIndex) {
    final CommandLine cmdLine = new CommandLine(COMMAND_QSUB);
    cmdLine.addArgument(PARAMETER_ARRAY_JOB_STATUS);
    String rangeArgument = beginIndex + "-" + endIndex;
    String listArgument = StringUtils.join(pbsArrayIDs, ",");
    String combinedArgument = listArgument + "," + rangeArgument;
    cmdLine.addArgument(combinedArgument);
    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.tupilabs.pbs.PBS.java

/**
 * PBS qstat command for Array Jobs//from  w w  w. j  av  a 2  s. c  o  m
 * <p>
 * Equivalent to qstat -f -t [param]
 *
 * @param name job name
 * @return list of jobs
 */
public static List<Job> qstatArrayJob(String name) {
    final CommandLine cmdLine = new CommandLine(COMMAND_QSTAT);
    cmdLine.addArgument(PARAMETER_FULL_STATUS);
    cmdLine.addArgument(PARAMETER_ARRAY_JOB_STATUS);
    if (StringUtils.isNotBlank(name)) {
        cmdLine.addArgument(name);
    }

    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 qstat command: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new PBSException("Failed to execute qstat command: " + e.getMessage(), e);
    } catch (InterruptedException e) {
        throw new PBSException("Failed to execute qstat command: " + e.getMessage(), e);
    }

    final int exitValue = resultHandler.getExitValue();
    LOGGER.info("qstat exit value: " + exitValue);

    final List<Job> jobs;
    try {
        jobs = QSTAT_JOBS_PARSER.parse(out.toString());
    } catch (ParseException pe) {
        throw new PBSException("Failed to parse qstat jobs output: " + pe.getMessage(), pe);
    }

    return (jobs == null ? new ArrayList<Job>(0) : jobs);
}

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

/**
 * PBS qsub command./*from www.ja  va  2 s. c o m*/
 * <p>
 * Equivalent to qsub [param]
 *
 * @param input job input file
 * @return job id
 */
public static String qsub(String input) {
    final CommandLine cmdLine = new CommandLine(COMMAND_QSUB);
    cmdLine.addArgument(input);

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

    DefaultExecuteResultHandler resultHandler;
    try {
        resultHandler = execute(cmdLine, null, 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.tupilabs.pbs.PBS.java

/**
 * PBS qsub command for an Array Job with Specific PBS_ARRAY_IDs to submit, and resource overrides
 * <p>//  w w  w.  j a v a  2 s  . c  o  m
 * Equivalent to qsub -t 1,2,3 -l [resource_name=value,resource_name=value] [param]
 *
 * @param input job input file
 * @param pbsArrayIDs of specified PBS indices
 * @param resourceOverrides list of resource overrides
 * @return job id of array job
 */
public static String qsubArrayJob(String input, List<Integer> pbsArrayIDs, String... resourceOverrides) {
    final CommandLine cmdLine = new CommandLine(COMMAND_QSUB);
    cmdLine.addArgument(PARAMETER_ARRAY_JOB_STATUS);
    String listArgument = StringUtils.join(pbsArrayIDs, ",");
    cmdLine.addArgument(listArgument);
    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:com.tupilabs.pbs.PBS.java

/**
 * PBS tracejob command./* w w  w.ja v a 2 s .  co m*/
 * <p>
 * Equivalent to tracejob -n [numberOfDays] [jobId]
 *
 * @param jobId job id
 * @param numberOfDays number of days to look for the job
 * @param quiet quiet mode flag
 * @return tracejob output
 */
public static CommandOutput traceJob(String jobId, int numberOfDays, boolean quiet) {
    final CommandLine cmdLine = new CommandLine(COMMAND_TRACEJOB);
    cmdLine.addArgument(PARAMETER_NUMBER_OF_DAYS);
    cmdLine.addArgument(Integer.toString(numberOfDays));
    if (quiet) {
        cmdLine.addArgument(PARAMETER_QUIET_MODE);
    }
    cmdLine.addArgument(jobId);

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

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

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

    return new CommandOutput(out.toString(), err.toString());
}

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

/**
 * PBS qstat command.//from w ww . j  a v a2s  . c  o  m
 * <p>
 * Equivalent to qstat -f [param]
 *
 * @param name job name
 * @return list of jobs
 */
public static List<Job> qstat(String name) {
    final CommandLine cmdLine = new CommandLine(COMMAND_QSTAT);
    cmdLine.addArgument(PARAMETER_FULL_STATUS);
    if (StringUtils.isNotBlank(name)) {
        cmdLine.addArgument(name);
    }

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

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

    final int exitValue = resultHandler.getExitValue();
    LOGGER.info("qstat exit value: " + exitValue);

    final List<Job> jobs;
    try {
        jobs = QSTAT_JOBS_PARSER.parse(out.toString());
    } catch (ParseException pe) {
        throw new PBSException("Failed to parse qstat jobs output: " + pe.getMessage(), pe);
    }

    return (jobs == null ? new ArrayList<Job>(0) : jobs);
}