Java Code Examples for org.apache.hadoop.util.Shell.ShellCommandExecutor#getExitCode()

The following examples show how to use org.apache.hadoop.util.Shell.ShellCommandExecutor#getExitCode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileUtil.java    From hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * Change the permissions on a file / directory, recursively, if
 * needed.
 * @param filename name of the file whose permissions are to change
 * @param perm permission string
 * @param recursive true, if permissions should be changed recursively
 * @return the exit code from the command.
 * @throws IOException
 */
public static int chmod(String filename, String perm, boolean recursive)
                          throws IOException {
  String [] cmd = Shell.getSetPermissionCommand(perm, recursive);
  String[] args = new String[cmd.length + 1];
  System.arraycopy(cmd, 0, args, 0, cmd.length);
  args[cmd.length] = new File(filename).getPath();
  ShellCommandExecutor shExec = new ShellCommandExecutor(args);
  try {
    shExec.execute();
  }catch(IOException e) {
    if(LOG.isDebugEnabled()) {
      LOG.debug("Error while changing permission : " + filename 
                +" Exception: " + StringUtils.stringifyException(e));
    }
  }
  return shExec.getExitCode();
}
 
Example 2
Source File: FileUtil.java    From RDFS with Apache License 2.0 6 votes vote down vote up
/**
 * Change the permissions on a file / directory, recursively, if
 * needed.
 * @param filename name of the file whose permissions are to change
 * @param perm permission string
 * @param recursive true, if permissions should be changed recursively
 * @return the exit code from the command.
 * @throws IOException
 * @throws InterruptedException
 */
public static int chmod(String filename, String perm, boolean recursive)
                          throws IOException, InterruptedException {
  StringBuffer cmdBuf = new StringBuffer();
  cmdBuf.append("chmod ");
  if (recursive) {
    cmdBuf.append("-R ");
  }
  cmdBuf.append(perm).append(" ");
  cmdBuf.append(filename);
  String[] shellCmd = {"bash", "-c" ,cmdBuf.toString()};
  ShellCommandExecutor shExec = new ShellCommandExecutor(shellCmd);
  try {
    shExec.execute();
  }catch(IOException e) {
    if(Log.isDebugEnabled()) {
      Log.debug("Error while changing permission : " + filename 
          +" Exception: " + StringUtils.stringifyException(e));
    }
  }
  return shExec.getExitCode();
}
 
Example 3
Source File: FileUtil.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Change the permissions on a file / directory, recursively, if
 * needed.
 * @param filename name of the file whose permissions are to change
 * @param perm permission string
 * @param recursive true, if permissions should be changed recursively
 * @return the exit code from the command.
 * @throws IOException exception on chmod
 */
public static int chmod(String filename, String perm, boolean recursive)
    throws IOException {
  String [] cmd = Shell.getSetPermissionCommand(perm, recursive);
  String[] args = new String[cmd.length + 1];
  System.arraycopy(cmd, 0, args, 0, cmd.length);
  args[cmd.length] = new File(filename).getPath();
  ShellCommandExecutor shExec = new ShellCommandExecutor(args);
  try {
    shExec.execute();
  }catch(IOException e) {
    if(LOG.isDebugEnabled()) {
      LOG.debug("Error while changing permission : {} Exception: {}", filename, StringUtils.stringifyException(e));
    }
  }
  return shExec.getExitCode();
}
 
Example 4
Source File: FileUtil.java    From big-c with Apache License 2.0 6 votes vote down vote up
/**
 * Change the permissions on a file / directory, recursively, if
 * needed.
 * @param filename name of the file whose permissions are to change
 * @param perm permission string
 * @param recursive true, if permissions should be changed recursively
 * @return the exit code from the command.
 * @throws IOException
 */
public static int chmod(String filename, String perm, boolean recursive)
                          throws IOException {
  String [] cmd = Shell.getSetPermissionCommand(perm, recursive);
  String[] args = new String[cmd.length + 1];
  System.arraycopy(cmd, 0, args, 0, cmd.length);
  args[cmd.length] = new File(filename).getPath();
  ShellCommandExecutor shExec = new ShellCommandExecutor(args);
  try {
    shExec.execute();
  }catch(IOException e) {
    if(LOG.isDebugEnabled()) {
      LOG.debug("Error while changing permission : " + filename 
                +" Exception: " + StringUtils.stringifyException(e));
    }
  }
  return shExec.getExitCode();
}
 
Example 5
Source File: LinuxContainerExecutor.java    From big-c with Apache License 2.0 6 votes vote down vote up
public void mountCgroups(List<String> cgroupKVs, String hierarchy)
       throws IOException {
  List<String> command = new ArrayList<String>(
          Arrays.asList(containerExecutorExe, "--mount-cgroups", hierarchy));
  command.addAll(cgroupKVs);
  
  String[] commandArray = command.toArray(new String[command.size()]);
  ShellCommandExecutor shExec = new ShellCommandExecutor(commandArray);

  if (LOG.isDebugEnabled()) {
      LOG.debug("mountCgroups: " + Arrays.toString(commandArray));
  }

  try {
      shExec.execute();
  } catch (IOException e) {
      int ret_code = shExec.getExitCode();
      LOG.warn("Exception in LinuxContainerExecutor mountCgroups ", e);
      logOutput(shExec.getOutput());
      throw new IOException("Problem mounting cgroups " + cgroupKVs + 
        "; exit code = " + ret_code + " and output: " + shExec.getOutput(), e);
  }
}
 
Example 6
Source File: LinuxContainerExecutor.java    From big-c with Apache License 2.0 6 votes vote down vote up
@Override 
public void init() throws IOException {        
  // Send command to executor which will just start up, 
  // verify configuration/permissions and exit
  List<String> command = new ArrayList<String>(
      Arrays.asList(containerExecutorExe,
          "--checksetup"));
  String[] commandArray = command.toArray(new String[command.size()]);
  ShellCommandExecutor shExec = new ShellCommandExecutor(commandArray);
  if (LOG.isDebugEnabled()) {
    LOG.debug("checkLinuxExecutorSetup: " + Arrays.toString(commandArray));
  }
  try {
    shExec.execute();
  } catch (ExitCodeException e) {
    int exitCode = shExec.getExitCode();
    LOG.warn("Exit code from container executor initialization is : "
        + exitCode, e);
    logOutput(shExec.getOutput());
    throw new IOException("Linux container executor not configured properly"
        + " (error=" + exitCode + ")", e);
  }
 
  resourcesHandler.init(this);
}
 
Example 7
Source File: LinuxContainerExecutor.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Override
public boolean signalContainer(String user, String pid, Signal signal)
    throws IOException {

  verifyUsernamePattern(user);
  String runAsUser = getRunAsUser(user);

  String[] command =
      new String[] { containerExecutorExe,
                 runAsUser,
                 user,
                 Integer.toString(Commands.SIGNAL_CONTAINER.getValue()),
                 pid,
                 Integer.toString(signal.getValue()) };
  ShellCommandExecutor shExec = new ShellCommandExecutor(command);
  if (LOG.isDebugEnabled()) {
    LOG.debug("signalContainer: " + Arrays.toString(command));
  }
  try {
    shExec.execute();
  } catch (ExitCodeException e) {
    int ret_code = shExec.getExitCode();
    if (ret_code == ResultCode.INVALID_CONTAINER_PID.getValue()) {
      return false;
    }
    LOG.warn("Error in signalling container " + pid + " with " + signal
        + "; exit = " + ret_code, e);
    logOutput(shExec.getOutput());
    throw new IOException("Problem signalling container " + pid + " with "
        + signal + "; output: " + shExec.getOutput() + " and exitCode: "
        + ret_code, e);
  }
  return true;
}
 
Example 8
Source File: FileUtil.java    From big-c with Apache License 2.0 5 votes vote down vote up
private static void unTarUsingTar(File inFile, File untarDir,
    boolean gzipped) throws IOException {
  StringBuffer untarCommand = new StringBuffer();
  if (gzipped) {
    untarCommand.append(" gzip -dc '");
    untarCommand.append(FileUtil.makeShellPath(inFile));
    untarCommand.append("' | (");
  } 
  untarCommand.append("cd '");
  untarCommand.append(FileUtil.makeShellPath(untarDir)); 
  untarCommand.append("' ; ");
  untarCommand.append("tar -xf ");

  if (gzipped) {
    untarCommand.append(" -)");
  } else {
    untarCommand.append(FileUtil.makeShellPath(inFile));
  }
  String[] shellCmd = { "bash", "-c", untarCommand.toString() };
  ShellCommandExecutor shexec = new ShellCommandExecutor(shellCmd);
  shexec.execute();
  int exitcode = shexec.getExitCode();
  if (exitcode != 0) {
    throw new IOException("Error untarring file " + inFile + 
                ". Tar process exited with exit code " + exitcode);
  }
}
 
Example 9
Source File: FileUtil.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private static void unTarUsingTar(File inFile, File untarDir,
                                  boolean gzipped) throws IOException {
  StringBuffer untarCommand = new StringBuffer();
  if (gzipped) {
    untarCommand.append(" gzip -dc '");
    untarCommand.append(FileUtil.makeSecureShellPath(inFile));
    untarCommand.append("' | (");
  }
  untarCommand.append("cd '");
  untarCommand.append(FileUtil.makeSecureShellPath(untarDir));
  untarCommand.append("' && ");
  untarCommand.append("tar -xf ");

  if (gzipped) {
    untarCommand.append(" -)");
  } else {
    untarCommand.append(FileUtil.makeSecureShellPath(inFile));
  }
  String[] shellCmd = { "bash", "-c", untarCommand.toString() };
  ShellCommandExecutor shexec = new ShellCommandExecutor(shellCmd);
  shexec.execute();
  int exitcode = shexec.getExitCode();
  if (exitcode != 0) {
    throw new IOException("Error untarring file " + inFile +
        ". Tar process exited with exit code " + exitcode);
  }
}
 
Example 10
Source File: TaskTracker.java    From RDFS with Apache License 2.0 5 votes vote down vote up
/**
 * Runs the script given in args
 * @param args script name followed by its argumnets
 * @param dir current working directory.
 * @throws IOException
 */
public void runScript(List<String> args, File dir) throws IOException {
  ShellCommandExecutor shexec =
          new ShellCommandExecutor(args.toArray(new String[0]), dir);
  shexec.execute();
  int exitCode = shexec.getExitCode();
  if (exitCode != 0) {
    throw new IOException("Task debug script exit with nonzero status of "
                          + exitCode + ".");
  }
}
 
Example 11
Source File: FileUtil.java    From RDFS with Apache License 2.0 5 votes vote down vote up
/**
 * Given a Tar File as input it will untar the file in a the untar directory
 * passed as the second parameter
 * 
 * This utility will untar ".tar" files and ".tar.gz","tgz" files.
 *  
 * @param inFile The tar file as input. 
 * @param untarDir The untar directory where to untar the tar file.
 * @throws IOException
 */
public static void unTar(File inFile, File untarDir) throws IOException {
  if (!untarDir.mkdirs()) {           
    if (!untarDir.isDirectory()) {
      throw new IOException("Mkdirs failed to create " + untarDir);
    }
  }

  StringBuffer untarCommand = new StringBuffer();
  boolean gzipped = inFile.toString().endsWith("gz");
  if (gzipped) {
    untarCommand.append(" gzip -dc '");
    untarCommand.append(FileUtil.makeShellPath(inFile));
    untarCommand.append("' | (");
  } 
  untarCommand.append("cd '");
  untarCommand.append(FileUtil.makeShellPath(untarDir)); 
  untarCommand.append("' ; ");
  untarCommand.append("tar -xf ");
  
  if (gzipped) {
    untarCommand.append(" -)");
  } else {
    untarCommand.append(FileUtil.makeShellPath(inFile));
  }
  String[] shellCmd = { "bash", "-c", untarCommand.toString() };
  ShellCommandExecutor shexec = new ShellCommandExecutor(shellCmd);
  shexec.execute();
  int exitcode = shexec.getExitCode();
  if (exitcode != 0) {
    throw new IOException("Error untarring file " + inFile + 
                ". Tar process exited with exit code " + exitcode);
  }
}
 
Example 12
Source File: FileUtil.java    From hadoop with Apache License 2.0 5 votes vote down vote up
private static void unTarUsingTar(File inFile, File untarDir,
    boolean gzipped) throws IOException {
  StringBuffer untarCommand = new StringBuffer();
  if (gzipped) {
    untarCommand.append(" gzip -dc '");
    untarCommand.append(FileUtil.makeShellPath(inFile));
    untarCommand.append("' | (");
  } 
  untarCommand.append("cd '");
  untarCommand.append(FileUtil.makeShellPath(untarDir)); 
  untarCommand.append("' ; ");
  untarCommand.append("tar -xf ");

  if (gzipped) {
    untarCommand.append(" -)");
  } else {
    untarCommand.append(FileUtil.makeShellPath(inFile));
  }
  String[] shellCmd = { "bash", "-c", untarCommand.toString() };
  ShellCommandExecutor shexec = new ShellCommandExecutor(shellCmd);
  shexec.execute();
  int exitcode = shexec.getExitCode();
  if (exitcode != 0) {
    throw new IOException("Error untarring file " + inFile + 
                ". Tar process exited with exit code " + exitcode);
  }
}
 
Example 13
Source File: JvmManager.java    From hadoop-gpu with Apache License 2.0 5 votes vote down vote up
public void runChild(JvmEnv env) {
  try {
    env.vargs.add(Integer.toString(jvmId.getId()));
    List<String> wrappedCommand = 
      TaskLog.captureOutAndError(env.setup, env.vargs, env.stdout, env.stderr,
          env.logSize, env.pidFile);
    shexec = new ShellCommandExecutor(wrappedCommand.toArray(new String[0]), 
        env.workDir, env.env);
    shexec.execute();
  } catch (IOException ioe) {
    // do nothing
    // error and output are appropriately redirected
  } finally { // handle the exit code
    if (shexec == null) {
      return;
    }
    int exitCode = shexec.getExitCode();
    updateOnJvmExit(jvmId, exitCode, killed);
    LOG.info("JVM : " + jvmId +" exited. Number of tasks it ran: " + 
        numTasksRan);
    try {
      // In case of jvm-reuse,
      //the task jvm cleans up the common workdir for every 
      //task at the beginning of each task in the task JVM.
      //For the last task, we do it here.
      if (env.conf.getNumTasksToExecutePerJvm() != 1) {
        FileUtil.fullyDelete(env.workDir);
      }
    } catch (IOException ie){}
  }
}
 
Example 14
Source File: TaskTracker.java    From hadoop-gpu with Apache License 2.0 5 votes vote down vote up
/**
 * Runs the script given in args
 * @param args script name followed by its argumnets
 * @param dir current working directory.
 * @throws IOException
 */
public void runScript(List<String> args, File dir) throws IOException {
  ShellCommandExecutor shexec = 
          new ShellCommandExecutor(args.toArray(new String[0]), dir);
  shexec.execute();
  int exitCode = shexec.getExitCode();
  if (exitCode != 0) {
    throw new IOException("Task debug script exit with nonzero status of " 
                          + exitCode + ".");
  }
}
 
Example 15
Source File: FileUtil.java    From hadoop-gpu with Apache License 2.0 5 votes vote down vote up
/**
 * Given a Tar File as input it will untar the file in a the untar directory
 * passed as the second parameter
 * 
 * This utility will untar ".tar" files and ".tar.gz","tgz" files.
 *  
 * @param inFile The tar file as input. 
 * @param untarDir The untar directory where to untar the tar file.
 * @throws IOException
 */
public static void unTar(File inFile, File untarDir) throws IOException {
  if (!untarDir.mkdirs()) {           
    if (!untarDir.isDirectory()) {
      throw new IOException("Mkdirs failed to create " + untarDir);
    }
  }

  StringBuffer untarCommand = new StringBuffer();
  boolean gzipped = inFile.toString().endsWith("gz");
  if (gzipped) {
    untarCommand.append(" gzip -dc '");
    untarCommand.append(FileUtil.makeShellPath(inFile));
    untarCommand.append("' | (");
  } 
  untarCommand.append("cd '");
  untarCommand.append(FileUtil.makeShellPath(untarDir)); 
  untarCommand.append("' ; ");
  untarCommand.append("tar -xf ");
  
  if (gzipped) {
    untarCommand.append(" -)");
  } else {
    untarCommand.append(FileUtil.makeShellPath(inFile));
  }
  String[] shellCmd = { "bash", "-c", untarCommand.toString() };
  ShellCommandExecutor shexec = new ShellCommandExecutor(shellCmd);
  shexec.execute();
  int exitcode = shexec.getExitCode();
  if (exitcode != 0) {
    throw new IOException("Error untarring file " + inFile + 
                ". Tar process exited with exit code " + exitcode);
  }
}
 
Example 16
Source File: LinuxContainerExecutor.java    From big-c with Apache License 2.0 4 votes vote down vote up
@Override
public void deleteAsUser(String user, Path dir, Path... baseDirs) {
  verifyUsernamePattern(user);
  String runAsUser = getRunAsUser(user);

  String dirString = dir == null ? "" : dir.toUri().getPath();

  List<String> command = new ArrayList<String>(
      Arrays.asList(containerExecutorExe,
                  runAsUser,
                  user,
                  Integer.toString(Commands.DELETE_AS_USER.getValue()),
                  dirString));
  List<String> pathsToDelete = new ArrayList<String>();
  if (baseDirs == null || baseDirs.length == 0) {
    LOG.info("Deleting absolute path : " + dir);
    pathsToDelete.add(dirString);
  } else {
    for (Path baseDir : baseDirs) {
      Path del = dir == null ? baseDir : new Path(baseDir, dir);
      LOG.info("Deleting path : " + del);
      pathsToDelete.add(del.toString());
      command.add(baseDir.toUri().getPath());
    }
  }
  String[] commandArray = command.toArray(new String[command.size()]);
  ShellCommandExecutor shExec = new ShellCommandExecutor(commandArray);
  if (LOG.isDebugEnabled()) {
    LOG.debug("deleteAsUser: " + Arrays.toString(commandArray));
  }
  try {
    shExec.execute();
    if (LOG.isDebugEnabled()) {
      logOutput(shExec.getOutput());
    }
  } catch (IOException e) {
    int exitCode = shExec.getExitCode();
    LOG.error("DeleteAsUser for " + StringUtils.join(" ", pathsToDelete)
        + " returned with exit code: " + exitCode, e);
    LOG.error("Output from LinuxContainerExecutor's deleteAsUser follows:");
    logOutput(shExec.getOutput());
  }
}
 
Example 17
Source File: LinuxContainerExecutor.java    From big-c with Apache License 2.0 4 votes vote down vote up
@Override
public void startLocalizer(Path nmPrivateContainerTokensPath,
    InetSocketAddress nmAddr, String user, String appId, String locId,
    LocalDirsHandlerService dirsHandler)
    throws IOException, InterruptedException {

  List<String> localDirs = dirsHandler.getLocalDirs();
  List<String> logDirs = dirsHandler.getLogDirs();
  
  verifyUsernamePattern(user);
  String runAsUser = getRunAsUser(user);
  List<String> command = new ArrayList<String>();
  addSchedPriorityCommand(command);
  command.addAll(Arrays.asList(containerExecutorExe, 
                 runAsUser,
                 user, 
                 Integer.toString(Commands.INITIALIZE_CONTAINER.getValue()),
                 appId,
                 nmPrivateContainerTokensPath.toUri().getPath().toString(),
                 StringUtils.join(",", localDirs),
                 StringUtils.join(",", logDirs)));

  File jvm =                                  // use same jvm as parent
    new File(new File(System.getProperty("java.home"), "bin"), "java");
  command.add(jvm.toString());
  command.add("-classpath");
  command.add(System.getProperty("java.class.path"));
  String javaLibPath = System.getProperty("java.library.path");
  if (javaLibPath != null) {
    command.add("-Djava.library.path=" + javaLibPath);
  }
  buildMainArgs(command, user, appId, locId, nmAddr, localDirs);
  String[] commandArray = command.toArray(new String[command.size()]);
  ShellCommandExecutor shExec = new ShellCommandExecutor(commandArray);
  if (LOG.isDebugEnabled()) {
    LOG.debug("initApplication: " + Arrays.toString(commandArray));
  }
  try {
    shExec.execute();
    if (LOG.isDebugEnabled()) {
      logOutput(shExec.getOutput());
    }
  } catch (ExitCodeException e) {
    int exitCode = shExec.getExitCode();
    LOG.warn("Exit code from container " + locId + " startLocalizer is : "
        + exitCode, e);
    logOutput(shExec.getOutput());
    throw new IOException("Application " + appId + " initialization failed" +
    		" (exitCode=" + exitCode + ") with output: " + shExec.getOutput(), e);
  }
}