Example usage for org.apache.hadoop.util Shell execCommand

List of usage examples for org.apache.hadoop.util Shell execCommand

Introduction

In this page you can find the example usage for org.apache.hadoop.util Shell execCommand.

Prototype


public static String execCommand(Map<String, String> env, String[] cmd, long timeout) throws IOException 

Source Link

Document

Static method to execute a shell command.

Usage

From source file:ApplicationMaster.java

License:Apache License

/**
 * Dump out contents of $CWD and the environment to stdout for debugging
 *///from  w  w  w  .ja  v  a2 s.c  o  m
private void dumpOutDebugInfo() {

    LOG.info("Dump debug output");
    Map<String, String> envs = System.getenv();
    for (Map.Entry<String, String> env : envs.entrySet()) {
        LOG.info("System env: key=" + env.getKey() + ", val=" + env.getValue());
        System.out.println("System env: key=" + env.getKey() + ", val=" + env.getValue());
    }

    BufferedReader buf = null;
    try {
        String lines = Shell.WINDOWS ? Shell.execCommand("cmd", "/c", "dir") : Shell.execCommand("ls", "-al");
        buf = new BufferedReader(new StringReader(lines));
        String line = "";
        while ((line = buf.readLine()) != null) {
            LOG.info("System CWD content: " + line);
            System.out.println("System CWD content: " + line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.cleanup(LOG, buf);
    }
}

From source file:io.hops.tensorflow.ApplicationMaster.java

License:Apache License

/**
 * Dump out contents of $CWD and the environment to stdout for debugging
 *//*  w w w.ja  v  a  2 s  .c o  m*/
private void dumpOutDebugInfo() {
    LOG.info("Dump debug output");
    Map<String, String> envs = System.getenv();
    for (Map.Entry<String, String> env : envs.entrySet()) {
        LOG.info("System env: key=" + env.getKey() + ", val=" + env.getValue());
        System.out.println("System env: key=" + env.getKey() + ", val=" + env.getValue());
    }

    BufferedReader buf = null;
    try {
        String lines = Shell.WINDOWS ? Shell.execCommand("cmd", "/c", "dir") : Shell.execCommand("ls", "-al");
        buf = new BufferedReader(new StringReader(lines));
        String line = "";
        while ((line = buf.readLine()) != null) {
            LOG.info("System CWD content: " + line);
            System.out.println("System CWD content: " + line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.cleanup(LOG, buf);
    }
}

From source file:org.apache.hive.jdbc.TestSSL.java

License:Apache License

private int execCommand(String cmd) throws Exception {
    int exitCode;
    try {//from ww w  .  j  a  v a  2s. c om
        String output = Shell.execCommand("bash", "-c", cmd);
        LOG.info("Output from '" + cmd + "': " + output);
        exitCode = 0;
    } catch (Shell.ExitCodeException e) {
        exitCode = e.getExitCode();
        LOG.info("Error executing '" + cmd + "', exitCode = " + exitCode, e);
    }
    return exitCode;
}

From source file:org.apache.sqoop.util.DirectImportUtils.java

License:Apache License

/**
 * Executes chmod on the specified file, passing in the mode string 'modstr'
 * which may be e.g. "a+x" or "0600", etc.
 * @throws IOException if chmod failed./*from   ww w .java2 s.c  o  m*/
 */
public static void setFilePermissions(File file, String modstr) throws IOException {
    // Set this file to be 0600. Java doesn't have a built-in mechanism for this
    // so we need to go out to the shell to execute chmod.
    try {
        Shell.execCommand("chmod", modstr, file.toString());
    } catch (IOException ioe) {
        // Shell.execCommand will throw IOException on exit code != 0.
        LOG.error("Could not chmod " + modstr + " " + file.toString());
        throw new IOException("Could not ensure password file security.", ioe);
    }
}