use of org.apache.commons.exec.DefaultExecutor in project zeppelin by apache.
the class ShellInterpreter method interpret.
@Override
public InterpreterResult interpret(String cmd, InterpreterContext contextInterpreter) {
LOGGER.debug("Run shell command '" + cmd + "'");
OutputStream outStream = new ByteArrayOutputStream();
CommandLine cmdLine = CommandLine.parse(shell);
// they need to be delimited by '&&' instead
if (isWindows) {
String[] lines = StringUtils.split(cmd, "\n");
cmd = StringUtils.join(lines, " && ");
}
cmdLine.addArgument(cmd, false);
try {
DefaultExecutor executor = new DefaultExecutor();
executor.setStreamHandler(new PumpStreamHandler(contextInterpreter.out, contextInterpreter.out));
executor.setWatchdog(new ExecuteWatchdog(Long.valueOf(getProperty(TIMEOUT_PROPERTY))));
executors.put(contextInterpreter.getParagraphId(), executor);
int exitVal = executor.execute(cmdLine);
LOGGER.info("Paragraph " + contextInterpreter.getParagraphId() + " return with exit value: " + exitVal);
return new InterpreterResult(Code.SUCCESS, outStream.toString());
} catch (ExecuteException e) {
int exitValue = e.getExitValue();
LOGGER.error("Can not run " + cmd, e);
Code code = Code.ERROR;
String message = outStream.toString();
if (exitValue == 143) {
code = Code.INCOMPLETE;
message += "Paragraph received a SIGTERM\n";
LOGGER.info("The paragraph " + contextInterpreter.getParagraphId() + " stopped executing: " + message);
}
message += "ExitValue: " + exitValue;
return new InterpreterResult(code, message);
} catch (IOException e) {
LOGGER.error("Can not run " + cmd, e);
return new InterpreterResult(Code.ERROR, e.getMessage());
} finally {
executors.remove(contextInterpreter.getParagraphId());
}
}
use of org.apache.commons.exec.DefaultExecutor in project hive by apache.
the class ExecServiceImpl method auxRun.
private ExecBean auxRun(String program, List<String> args, Map<String, String> env) throws NotAuthorizedException, ExecuteException, IOException {
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValues(null);
// Setup stdout and stderr
int nbytes = appConf.getInt(AppConfig.EXEC_MAX_BYTES_NAME, -1);
ByteArrayOutputStream outStream = new MaxByteArrayOutputStream(nbytes);
ByteArrayOutputStream errStream = new MaxByteArrayOutputStream(nbytes);
executor.setStreamHandler(new PumpStreamHandler(outStream, errStream));
// Only run for N milliseconds
int timeout = appConf.getInt(AppConfig.EXEC_TIMEOUT_NAME, 0);
ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
executor.setWatchdog(watchdog);
CommandLine cmd = makeCommandLine(program, args);
LOG.info("Running: " + cmd);
ExecBean res = new ExecBean();
res.exitcode = executor.execute(cmd, execEnv(env));
String enc = appConf.get(AppConfig.EXEC_ENCODING_NAME);
res.stdout = outStream.toString(enc);
res.stderr = errStream.toString(enc);
try {
watchdog.checkException();
} catch (Exception ex) {
LOG.error("Command: " + cmd + " failed. res=" + res, ex);
}
if (watchdog.killedProcess()) {
String msg = " was terminated due to timeout(" + timeout + "ms). See " + AppConfig.EXEC_TIMEOUT_NAME + " property";
LOG.warn("Command: " + cmd + msg + " res=" + res);
res.stderr += " Command " + msg;
}
if (res.exitcode != 0) {
LOG.info("Command: " + cmd + " failed. res=" + res);
}
return res;
}
use of org.apache.commons.exec.DefaultExecutor in project camel by apache.
the class DefaultExecCommandExecutor method prepareDefaultExecutor.
protected DefaultExecutor prepareDefaultExecutor(ExecCommand execCommand) {
DefaultExecutor executor = new ExecDefaultExecutor();
executor.setExitValues(null);
if (execCommand.getWorkingDir() != null) {
executor.setWorkingDirectory(new File(execCommand.getWorkingDir()).getAbsoluteFile());
}
if (execCommand.getTimeout() != ExecEndpoint.NO_TIMEOUT) {
executor.setWatchdog(new ExecuteWatchdog(execCommand.getTimeout()));
}
executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
return executor;
}
use of org.apache.commons.exec.DefaultExecutor in project robovm by robovm.
the class Executor method execCapture.
public String execCapture() throws IOException {
ExecuteStreamHandler oldStreamHandler = streamHandler;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
CommandLine commandLine = generateCommandLine();
try {
streamHandler(new PumpStreamHandler(baos));
logCommandLine(commandLine);
DefaultExecutor executor = initExecutor(new DefaultExecutor());
executor.execute(commandLine, generateEnv());
return new String(baos.toByteArray()).trim();
} catch (ExecuteException e) {
String output = new String(baos.toByteArray()).trim();
if (output.length() > 0 && e.getMessage().startsWith("Process exited with an error")) {
StackTraceElement[] origStackTrace = e.getStackTrace();
e = new ExecuteException("Command '" + commandLine + "' failed with output: " + output + " ", e.getExitValue());
e.setStackTrace(origStackTrace);
}
throw e;
} finally {
streamHandler = oldStreamHandler;
}
}
use of org.apache.commons.exec.DefaultExecutor in project kylo by Teradata.
the class MultiUserProcessManager method refreshKerberosTicket.
/**
* Calls kinit to request a new Kerberos ticket if the previous one is about to expire.
*/
private void refreshKerberosTicket() {
// Determine if a new ticket is needed
if (kerberos == null || kerberos.getInitInterval() <= 0 || kerberosNextInit > DateTimeUtils.currentTimeMillis()) {
return;
}
// Build executor
final Executor executor = new DefaultExecutor();
final ShutdownHookProcessDestroyer processDestroyer = new ShutdownHookProcessDestroyer();
executor.setProcessDestroyer(processDestroyer);
final Logger outputLogger = LoggerFactory.getLogger(getClass().getName() + ".kinit");
final LoggerOutputStream outputStream = new LoggerOutputStream(outputLogger);
final PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
executor.setStreamHandler(streamHandler);
final ExecuteWatchdog watchdog = new ExecuteWatchdog(TimeUnit.SECONDS.toMillis(kerberos.getInitTimeout()));
executor.setWatchdog(watchdog);
// Run kinit to acquire a new ticket
final CommandLine command = new CommandLine("kinit").addArgument("-kt").addArgument(kerberos.getKeytabLocation()).addArgument(kerberos.getKerberosPrincipal());
log.debug("Acquiring a new Kerberos ticket with command: {}", command);
int exitCode;
try {
exitCode = executor.execute(command);
} catch (final IOException e) {
log.error("Failed to execute kinit", e);
exitCode = -1;
}
// Record next time to acquire ticket
if (!executor.isFailure(exitCode)) {
kerberosNextInit = DateTimeUtils.currentTimeMillis() + TimeUnit.SECONDS.toMillis(kerberos.getInitInterval());
} else {
if (watchdog.killedProcess()) {
log.error("Failed to acquire a Kerberos ticket within the allotted time: {}", kerberos.getInitTimeout());
} else {
log.error("Kinit exited with non-zero status: {}", exitCode);
}
kerberosNextInit = DateTimeUtils.currentTimeMillis() + TimeUnit.SECONDS.toMillis(kerberos.getRetryInterval());
throw new IllegalStateException("Failed to acquire a Kerberos ticket");
}
}
Aggregations