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

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

Introduction

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

Prototype

public int getExitValue() 

Source Link

Document

Get the exitValue of the process.

Usage

From source file:io.manasobi.utils.CmdUtils.java

/**
 *   (commandLine) ?   ?? ?  ? ?/*from ww w  .  j  a  v a  2s .com*/
 * 
 * @param commandLine    
 * @param argument     ?
 * @param timeout     
 * 
 * @return int ? 
 */
public static int execute(CommandLine commandLine, String argument, long timeout) {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(baos);

    DefaultExecutor executor = new DefaultExecutor();
    executor.setStreamHandler(streamHandler);

    // ? 
    if (StringUtils.isNotEmpty(argument)) {
        commandLine.addArguments(argument);
    }

    //  
    if (timeout > 0) {
        ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
        executor.setWatchdog(watchdog);
    }

    // ??  ? 
    executor.setExitValue(SUCCESS_RETURN_CODE);

    try {

        DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();

        executor.execute(commandLine, resultHandler);
        resultHandler.waitFor();

        return resultHandler.getExitValue();

    } catch (Exception e) {

        throw new CmdUtilsException(e.getMessage());
    }
}

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

/**
 * PBS qdel command./*from w ww. j  av  a2  s . c  o m*/
 * <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>//  w w  w .ja v a  2 s  .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 www.jav  a 2 s.  com*/
 * 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>//from w w w .j  ava2s  .c om
 * 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>/*  w w w. j  a  va2 s  .  co  m*/
 * 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 qsub command.//from  ww  w .ja  v  a2s .  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 ww. java  2  s .c  om*/
 * 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 qstat command for Array Jobs/*from w  w w.  jav  a 2s  . co 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 qstat command.// www  .  j av a 2 s .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);
}