Example usage for java.lang ProcessBuilder start

List of usage examples for java.lang ProcessBuilder start

Introduction

In this page you can find the example usage for java.lang ProcessBuilder start.

Prototype

public Process start() throws IOException 

Source Link

Document

Starts a new process using the attributes of this process builder.

Usage

From source file:com.tascape.qa.th.Utils.java

/**
 * Executes command, and waits for the expected pass/fail phrase in console printout within timeout,
 *
 * @param command    command line//from w  w  w.j  a v a  2  s  . c  o m
 * @param pass       skip checking if null
 * @param fail       skip checking if null
 * @param timeout    set 0 for not to check the output message, otherwise, waiting for timeout
 * @param workingDir working directory
 *
 * @return console output as a list of strings
 *
 * @throws IOException          for command error
 * @throws InterruptedException when interrupted
 */
public static List<String> cmd(String[] command, final String pass, final String fail, final long timeout,
        final String workingDir) throws IOException, InterruptedException {
    ProcessBuilder pb = new ProcessBuilder(command);
    if (workingDir != null) {
        pb.directory(new File(workingDir));
    }
    pb.redirectErrorStream(true);
    LOG.debug("Running command: " + pb.command().toString().replace(",", ""));
    final Process p = pb.start();
    Thread thread;
    final List<String> output = new ArrayList<>();

    if (timeout == 0) {
        LOG.debug("This is a start-and-exit command");
        output.add(PASS);
        return output;
    } else {
        thread = new Thread() {
            @Override
            public void run() {
                try {
                    LOG.debug("Command timeouts in {} ms", timeout);
                    Thread.sleep(timeout);
                    try {
                        p.exitValue();
                    } catch (IllegalThreadStateException ex) {
                        LOG.debug("killing subprocess {} - {}", p, ex.getMessage());
                        p.destroy();
                    }
                } catch (InterruptedException ex) {
                    LOG.warn(ex.getMessage());
                }
            }
        };
        thread.setName(Thread.currentThread().getName() + "-" + p.hashCode());
        thread.setDaemon(true);
        thread.start();
    }

    BufferedReader stdIn = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String c = p + " - ";
    for (String line = stdIn.readLine(); line != null;) {
        LOG.trace("{}{}", c, line);
        output.add(line);
        try {
            line = stdIn.readLine();
        } catch (IOException ex) {
            LOG.warn(ex.getMessage());
            break;
        }
    }
    LOG.debug("Command exit code {}", p.waitFor());
    thread.interrupt();
    try {
        stdIn.close();
    } catch (IOException ex) {
        LOG.warn("", ex);
    }

    for (String s : output) {
        if (pass != null && (s.contains(pass) || s.matches(pass))) {
            output.add(PASS);
            break;
        } else if (fail != null && s.contains(fail)) {
            output.add(FAIL);
            break;
        }
    }
    return output;
}

From source file:com.tascape.qa.th.Utils.java

public static Process cmd(String[] commands, final File file, final File workingDir,
        final String... ignoreRegex) throws IOException {
    FileUtils.touch(file);/*w ww.j  a  va 2s. c  o  m*/
    LOG.debug("Saving console output to {}", file.getAbsolutePath());

    ProcessBuilder pb = new ProcessBuilder(commands);
    pb.redirectErrorStream(true);
    pb.directory(workingDir);
    LOG.info("Running command {}:  {}", workingDir == null ? "" : workingDir.getAbsolutePath(),
            pb.command().toString().replaceAll(",", ""));
    final Process p = pb.start();

    Thread t = new Thread(Thread.currentThread().getName() + "-" + p.hashCode()) {
        @Override
        public void run() {
            BufferedReader stdIn = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String console = "console-" + stdIn.hashCode();
            try (PrintWriter pw = new PrintWriter(file)) {
                for (String line = stdIn.readLine(); line != null;) {
                    LOG.trace("{}: {}", console, line);
                    if (null == ignoreRegex || ignoreRegex.length == 0) {
                        pw.println(line);
                    } else {
                        boolean ignore = false;
                        for (String regex : ignoreRegex) {
                            if (!regex.isEmpty() && (line.contains(regex) || line.matches(regex))) {
                                ignore = true;
                                break;
                            }
                        }
                        if (!ignore) {
                            pw.println(line);
                        }
                    }
                    pw.flush();
                    line = stdIn.readLine();
                }
            } catch (IOException ex) {
                LOG.warn(ex.getMessage());
            }
            LOG.trace("command is done");
        }
    };
    t.setDaemon(true);
    t.start();

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            if (p != null) {
                p.destroy();
            }
        }
    });
    return p;
}

From source file:at.ac.tuwien.dsg.cloud.salsa.engine.utils.SystemFunctions.java

public static int executeCommandGetReturnCode(String cmd, String workingDir, String executeFrom) {
    if (workingDir == null) {
        workingDir = "/tmp";
    }//w w  w  . j  av  a 2s.c  o m
    logger.debug("Execute command: " + cmd + ". Working dir: " + workingDir);
    try {
        String[] splitStr = cmd.split("\\s+");
        ProcessBuilder pb = new ProcessBuilder(splitStr);
        pb.directory(new File(workingDir));
        pb = pb.redirectErrorStream(true); // this is important to redirect the error stream to output stream, prevent blocking with long output
        Map<String, String> env = pb.environment();
        String path = env.get("PATH");
        path = path + File.pathSeparator + "/usr/bin:/usr/sbin";
        logger.debug("PATH to execute command: " + pb.environment().get("PATH"));
        env.put("PATH", path);

        Process p = pb.start();

        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            logger.debug(line);
        }
        p.waitFor();
        int returnCode = p.exitValue();
        logger.debug("Execute command done: " + cmd + ". Get return code: " + returnCode);
        return returnCode;
    } catch (InterruptedException | IOException e1) {
        logger.error("Error when execute command. Error: " + e1);
    }
    return -1;
}

From source file:com.ibm.jaql.JaqlBaseTestCase.java

/**
 * Compares the tmp file and gold file using unix diff. whitespace is ignored
 * for the diff./*from  w ww .j  av a  2  s .  c  o m*/
 * 
 * 
 * @param tmpFileName tmp file name
 * @param goldFileName gold file name
 * @return <tt>true</tt> if the tmp file and gold file are the same;
 *         <tt>false</tt> otherwise.
 * @throws IOException
 */
public static boolean compareResults(String tmpFileName, String goldFileName) throws IOException {
    // use unix 'diff', ignoring whitespace
    ProcessBuilder pb = new ProcessBuilder("diff", "-w", tmpFileName, goldFileName);
    /*
     * Two input file for diff are the same only if nothing is printed to stdout
     * and stderr. Redirect stderr to stdout so that only stdout needs to
     * checked.
     */
    pb.redirectErrorStream(true);
    Process p = pb.start();
    InputStream str = p.getInputStream();

    byte[] b = new byte[1024];
    int numRead = 0;
    StringBuilder sb = new StringBuilder();
    while ((numRead = str.read(b)) > 0) {
        sb.append(new String(b, 0, numRead, "US-ASCII"));
    }
    if (sb.length() > 0)
        System.err.println("\ndiff -w " + tmpFileName + " " + goldFileName + "\n" + sb);

    return sb.length() == 0;
}

From source file:com.asakusafw.runtime.util.hadoop.ConfigurationProvider.java

private static File detectHadoopConfigurationDirectory(File command, File temporary, Map<String, String> envp)
        throws IOException {
    assert command != null;
    assert temporary != null;
    assert envp != null;

    prepareClasspath(temporary, ConfigurationDetecter.class);
    File resultOutput = new File(temporary, PATH_SUBPROC_OUTPUT);

    List<String> arguments = new ArrayList<>();
    arguments.add(command.getAbsolutePath());
    arguments.add(ConfigurationDetecter.class.getName());
    arguments.add(resultOutput.getAbsolutePath());

    ProcessBuilder processBuilder = new ProcessBuilder(arguments);
    processBuilder.environment().clear();
    processBuilder.environment().putAll(envp);
    processBuilder.environment().put(ENV_HADOOP_CLASSPATH, temporary.getPath());

    Process process = processBuilder.start();
    try {// w  w w.j a  va 2s .  co  m
        Thread redirectOut = redirect(process.getInputStream(), System.out);
        Thread redirectErr = redirect(process.getErrorStream(), System.err);
        try {
            int exit = process.waitFor();
            redirectOut.join();
            redirectErr.join();
            if (exit != 0) {
                throw new IOException(
                        MessageFormat.format("Failed to execute Hadoop command (exitcode={1}): {0}", arguments,
                                String.valueOf(exit)));
            }
        } catch (InterruptedException e) {
            throw (IOException) new InterruptedIOException(
                    MessageFormat.format("Failed to execute Hadoop command (interrupted): {0}", arguments))
                            .initCause(e);
        }
    } finally {
        process.destroy();
    }
    if (resultOutput.isFile() == false) {
        throw new IOException(
                MessageFormat.format("Failed to restore Hadoop configuration path: {0}", resultOutput));
    }
    File path = ConfigurationDetecter.read(resultOutput);
    return path;
}

From source file:at.ac.tuwien.dsg.cloud.salsa.engine.utils.SystemFunctions.java

/**
 * Run a command and wait/*from   w  w  w . j  a v  a 2s .  com*/
 *
 * @param cmd The command to run
 * @param workingDir The folder where the command is run
 * @param executeFrom For logging message to the center of where to execute the command.
 * @return
 */
public static String executeCommandGetOutput(String cmd, String workingDir, String executeFrom) {
    logger.debug("Execute command: " + cmd);
    if (workingDir == null) {
        workingDir = "/tmp";
    }
    try {
        String[] splitStr = cmd.split("\\s+");
        ProcessBuilder pb = new ProcessBuilder(splitStr);
        pb.directory(new File(workingDir));
        pb = pb.redirectErrorStream(true); // this is important to redirect the error stream to output stream, prevent blocking with long output
        Map<String, String> env = pb.environment();
        String path = env.get("PATH");
        path = path + File.pathSeparator + "/usr/bin:/usr/sbin";
        logger.debug("PATH to execute command: " + pb.environment().get("PATH"));
        env.put("PATH", path);

        Process p = pb.start();

        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        StringBuffer output = new StringBuffer();
        int lineCount = 0;
        while ((line = reader.readLine()) != null) {
            if (lineCount < 10) { // only get 10 lines to prevent the overflow
                output.append(line);
            }
            lineCount += 1;
            logger.debug(line);
        }
        if (lineCount >= 10) {
            logger.debug("... there are alot of more output here which is not shown ! ...");
        }
        p.waitFor();
        System.out.println("Execute Commang output: " + output.toString().trim());

        if (p.exitValue() == 0) {
            logger.debug("Command exit 0, result: " + output.toString().trim());
            return output.toString().trim();
        } else {
            logger.debug("Command return non zero code: " + p.exitValue());
            return null;
        }
    } catch (InterruptedException | IOException e1) {
        logger.error("Error when execute command. Error: " + e1);
    }
    return null;
}

From source file:com.kylinolap.metadata.tool.HiveSourceTableMgmt.java

/**
 * @param hiveCommd//w  ww  .jav a2s  . c  o m
 */
private static String callGenerateCommand(String hiveCommd) throws IOException {
    // Get out put path
    String tempDir = System.getProperty("java.io.tmpdir");
    logger.info("OS current temporary directory is " + tempDir);
    if (StringUtils.isEmpty(tempDir)) {
        tempDir = "/tmp";
    }
    String[] cmd = new String[2];
    String osName = System.getProperty("os.name");
    if (osName.startsWith("Windows")) {
        cmd[0] = "cmd.exe";
        cmd[1] = "/C";
    } else {
        cmd[0] = "/bin/bash";
        cmd[1] = "-c";
    }

    // hive command output
    // String hiveOutputPath = tempDir + File.separator +
    // "tmp_kylin_output";
    String hiveOutputPath = File.createTempFile("HiveOutput", null).getAbsolutePath();
    // Metadata output
    File dir = File.createTempFile("meta", null);
    dir.delete();
    dir.mkdir();
    String tableMetaOutcomeDir = dir.getAbsolutePath();

    ProcessBuilder pb = null;

    if (osName.startsWith("Windows")) {
        pb = new ProcessBuilder(cmd[0], cmd[1],
                "ssh root@sandbox 'hive -e \"" + hiveCommd + "\"' > " + hiveOutputPath);
    } else {
        pb = new ProcessBuilder(cmd[0], cmd[1], "hive -e \"" + hiveCommd + "\" > " + hiveOutputPath);
    }

    // Run hive
    pb.directory(new File(tempDir));
    pb.redirectErrorStream(true);
    Process p = pb.start();
    InputStream is = p.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = null;

    try {
        br = new BufferedReader(isr);
        String line = null;
        logger.info("Execute : " + pb.command().get(0));
        while ((line = br.readLine()) != null) {
            logger.info(line);
        }
    } finally {
        if (null != br) {
            br.close();
        }
    }
    logger.info("Hive execution completed!");

    HiveSourceTableMgmt rssMgmt = new HiveSourceTableMgmt();
    rssMgmt.extractTableDescFromFile(hiveOutputPath, tableMetaOutcomeDir);
    return tableMetaOutcomeDir;
}

From source file:at.ac.tuwien.dsg.cloud.salsa.engine.utils.SystemFunctions.java

/**
 * Run a command and wait//from w w w.  ja  va  2 s.  c  o  m
 *
 * @param cmd The command to run
 * @param workingDir The folder where the command is run
 * @param executeFrom For logging message to the center of where to execute the command.
 * @return
 */
public static Process executeCommandAndForget(String cmd, String workingDir, String executeFrom) {
    logger.debug("Execute command: " + cmd);
    if (workingDir == null) {
        workingDir = "/tmp";
    }

    String[] splitStr = cmd.split("\\s+");
    ProcessBuilder pb = new ProcessBuilder(splitStr);
    pb.directory(new File(workingDir));
    pb = pb.redirectErrorStream(true); // this is important to redirect the error stream to output stream, prevent blocking with long output
    pb.redirectOutput(new File("/tmp/salsa.conductor.log"));
    Map<String, String> env = pb.environment();
    String path = env.get("PATH");
    path = path + File.pathSeparator + "/usr/bin:/usr/sbin";
    logger.debug("PATH to execute command: " + pb.environment().get("PATH"));
    env.put("PATH", path);
    Process p;
    try {
        p = pb.start();
        return p;
    } catch (IOException ex) {
        logger.debug("Cannot run the command: " + cmd);
        return null;
    }

}

From source file:io.hops.hopsworks.common.util.HopsUtils.java

/**
 * Retrieves the global hadoop classpath.
 *
 * @param params/*w ww  . j  a  v  a2 s .  c  o  m*/
 * @return hadoop global classpath
 */
public static String getHadoopClasspathGlob(String... params) {
    ProcessBuilder pb = new ProcessBuilder(params);
    try {
        Process process = pb.start();
        int errCode = process.waitFor();
        if (errCode != 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        try (BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
        }
        //Now we must remove the yarn shuffle library as it creates issues for 
        //Zeppelin Spark Interpreter
        StringBuilder classpath = new StringBuilder();

        for (String path : sb.toString().split(File.pathSeparator)) {
            if (!path.contains("yarn") && !path.contains("jersey") && !path.contains("servlet")) {
                classpath.append(path).append(File.pathSeparator);
            }
        }
        if (classpath.length() > 0) {
            return classpath.toString().substring(0, classpath.length() - 1);
        }

    } catch (IOException | InterruptedException ex) {
        Logger.getLogger(HopsUtils.class.getName()).log(Level.SEVERE, null, ex);
    }
    return "";
}

From source file:gov.nist.appvet.tool.sigverifier.Service.java

private static boolean execute(String command, StringBuffer output) {
    List<String> commandArgs = Arrays.asList(command.split("\\s+"));
    ProcessBuilder pb = new ProcessBuilder(commandArgs);
    Process process = null;//from  w ww . j  a v  a  2 s . c o m
    IOThreadHandler outputHandler = null;
    IOThreadHandler errorHandler = null;
    int exitValue = -1;
    try {
        if (command == null || command.isEmpty()) {
            log.error("Command is null or empty");
            return false;
        }
        log.debug("Executing " + command);
        process = pb.start();
        outputHandler = new IOThreadHandler(process.getInputStream());
        outputHandler.start();
        errorHandler = new IOThreadHandler(process.getErrorStream());
        errorHandler.start();
        if (process.waitFor(Properties.commandTimeout, TimeUnit.MILLISECONDS)) {
            // Process has waited and exited within the timeout
            exitValue = process.exitValue();
            if (exitValue == 0) {
                log.debug("Command terminated normally: \n" + outputHandler.getOutput() + "\nErrors: "
                        + errorHandler.getOutput());
                StringBuffer resultOut = outputHandler.getOutput();
                output.append(resultOut);
                return true;
            } else {
                log.error("Command terminated abnormally: \n" + outputHandler.getOutput() + "\nErrors: "
                        + errorHandler.getOutput());
                StringBuffer resultError = errorHandler.getOutput();
                output.append(resultError);
                return false;
            }
        } else {
            // Process exceed timeout or was interrupted
            log.error("Command timed-out or was interrupted: \n" + outputHandler.getOutput() + "\nErrors: "
                    + errorHandler.getOutput());
            StringBuffer resultOutput = outputHandler.getOutput();
            StringBuffer resultError = errorHandler.getOutput();
            if (resultOutput != null) {
                output.append(resultOutput);
                return false;
            } else if (resultError != null) {
                output.append(resultError);
            } else {
                output.append(Properties.toolName + " timed-out");
            }
            return false;
        }
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } catch (InterruptedException e) {
        e.printStackTrace();
        return false;
    } finally {
        if (outputHandler.isAlive()) {
            try {
                outputHandler.inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (errorHandler.isAlive()) {
            try {
                errorHandler.inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (process.isAlive()) {
            process.destroy();
        }
    }
}