use of org.apache.commons.exec.ExecuteWatchdog in project frontend-maven-plugin by eirslett.
the class ProcessExecutor method createExecutor.
private Executor createExecutor(File workingDirectory, long timeoutInSeconds) {
DefaultExecutor executor = new DefaultExecutor();
executor.setWorkingDirectory(workingDirectory);
// Fixes #41
executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
if (timeoutInSeconds > 0) {
executor.setWatchdog(new ExecuteWatchdog(timeoutInSeconds * 1000));
}
return executor;
}
use of org.apache.commons.exec.ExecuteWatchdog in project openhab1-addons by openhab.
the class ExecBinding method executeCommandAndWaitResponse.
/**
* <p>
* Executes <code>commandLine</code>. Sometimes (especially observed on
* MacOS) the commandLine isn't executed properly. In that cases another
* exec-method is to be used. To accomplish this please use the special
* delimiter '<code>@@</code>'. If <code>commandLine</code> contains this
* delimiter it is split into a String[] array and the special exec-method
* is used.
* </p>
* <p>
* A possible {@link IOException} gets logged but no further processing is
* done.
* </p>
*
* @param commandLine the command line to execute
* @return response data from executed command line
*/
private String executeCommandAndWaitResponse(String commandLine) {
String retval = null;
CommandLine cmdLine = null;
if (commandLine.contains(CMD_LINE_DELIMITER)) {
String[] cmdArray = commandLine.split(CMD_LINE_DELIMITER);
cmdLine = new CommandLine(cmdArray[0]);
for (int i = 1; i < cmdArray.length; i++) {
cmdLine.addArgument(cmdArray[i], false);
}
} else {
cmdLine = CommandLine.parse(commandLine);
}
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
Executor executor = new DefaultExecutor();
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
PumpStreamHandler streamHandler = new PumpStreamHandler(stdout);
executor.setExitValue(1);
executor.setStreamHandler(streamHandler);
executor.setWatchdog(watchdog);
try {
executor.execute(cmdLine, resultHandler);
logger.debug("executed commandLine '{}'", commandLine);
} catch (ExecuteException e) {
logger.error("couldn't execute commandLine '" + commandLine + "'", e);
} catch (IOException e) {
logger.error("couldn't execute commandLine '" + commandLine + "'", e);
}
// can safely request the exit code
try {
resultHandler.waitFor();
int exitCode = resultHandler.getExitValue();
retval = StringUtils.chomp(stdout.toString());
logger.debug("exit code '{}', result '{}'", exitCode, retval);
} catch (InterruptedException e) {
logger.error("Timeout occured when executing commandLine '" + commandLine + "'", e);
}
return retval;
}
use of org.apache.commons.exec.ExecuteWatchdog in project opennms by OpenNMS.
the class SystemReportResourceLocator method slurpOutput.
@Override
public String slurpOutput(final String commandString, final boolean ignoreExitCode) {
final CommandLine command = CommandLine.parse(commandString);
LOG.debug("running: {}", commandString);
final Map<String, String> environment = new HashMap<String, String>(System.getenv());
environment.put("COLUMNS", "2000");
DataInputStream input = null;
PipedInputStream pis = null;
OutputSuckingParser parser = null;
String outputText = null;
final DefaultExecutor executor = new DefaultExecutor();
final PipedOutputStream output = new PipedOutputStream();
final PumpStreamHandler streamHandler = new PumpStreamHandler(output, output);
executor.setWatchdog(new ExecuteWatchdog(m_maxProcessWait));
executor.setStreamHandler(streamHandler);
if (ignoreExitCode) {
executor.setExitValues(null);
}
try {
LOG.trace("executing '{}'", commandString);
pis = new PipedInputStream(output);
input = new DataInputStream(pis);
parser = new OutputSuckingParser(input);
parser.start();
final int exitValue = executor.execute(command, environment);
IOUtils.closeQuietly(output);
parser.join(m_maxProcessWait);
if (!ignoreExitCode && exitValue != 0) {
LOG.debug("error running '{}': exit value was {}", commandString, exitValue);
} else {
outputText = parser.getOutput();
}
LOG.trace("finished '{}'", commandString);
} catch (final Exception e) {
LOG.debug("Failed to run '{}'", commandString, e);
} finally {
IOUtils.closeQuietly(output);
IOUtils.closeQuietly(input);
IOUtils.closeQuietly(pis);
}
return outputText;
}
use of org.apache.commons.exec.ExecuteWatchdog in project opennms by OpenNMS.
the class AbstractSystemReportPlugin method getOpenNMSProcesses.
protected Set<Integer> getOpenNMSProcesses() {
LOG.trace("getOpenNMSProcesses()");
final Set<Integer> processes = new HashSet<Integer>();
final String jps = getResourceLocator().findBinary("jps");
LOG.trace("jps = {}", jps);
DataInputStream input = null;
PsParser parser = null;
PipedInputStream pis = null;
PipedOutputStream output = new PipedOutputStream();
DefaultExecutor executor = new DefaultExecutor();
executor.setWatchdog(new ExecuteWatchdog(5000));
if (jps != null) {
CommandLine command = CommandLine.parse(jps + " -v");
PumpStreamHandler streamHandler = new PumpStreamHandler(output, System.err);
try {
LOG.trace("executing '{}'", command);
pis = new PipedInputStream(output);
input = new DataInputStream(pis);
parser = new PsParser(input, "opennms_bootstrap.jar", "status", 0);
parser.start();
executor.setStreamHandler(streamHandler);
int exitValue = executor.execute(command);
IOUtils.closeQuietly(output);
parser.join();
processes.addAll(parser.getProcesses());
LOG.trace("finished '{}'", command);
if (exitValue != 0) {
LOG.debug("error running '{}': exit value was {}", command, exitValue);
}
} catch (final Exception e) {
LOG.debug("Failed to run '{}'", command, e);
} finally {
IOUtils.closeQuietly(input);
IOUtils.closeQuietly(pis);
IOUtils.closeQuietly(output);
}
}
LOG.trace("looking for ps");
final String ps = getResourceLocator().findBinary("ps");
if (ps != null) {
// try Linux/Mac style
CommandLine command = CommandLine.parse(ps + " aww -o pid -o args");
output = new PipedOutputStream();
PumpStreamHandler streamHandler = new PumpStreamHandler(output, System.err);
try {
LOG.trace("executing '{}'", command);
pis = new PipedInputStream(output);
input = new DataInputStream(pis);
parser = new PsParser(input, "opennms_bootstrap.jar", "status", 0);
parser.start();
executor.setStreamHandler(streamHandler);
int exitValue = executor.execute(command);
IOUtils.closeQuietly(output);
parser.join(MAX_PROCESS_WAIT);
processes.addAll(parser.getProcesses());
LOG.trace("finished '{}'", command);
if (exitValue != 0) {
LOG.debug("error running '{}': exit value was {}", command, exitValue);
}
} catch (final Exception e) {
LOG.debug("error running '{}'", command, e);
} finally {
IOUtils.closeQuietly(input);
IOUtils.closeQuietly(pis);
IOUtils.closeQuietly(output);
}
if (processes.size() == 0) {
// try Solaris style
command = CommandLine.parse(ps + " -ea -o pid -o args");
output = new PipedOutputStream();
streamHandler = new PumpStreamHandler(output, System.err);
try {
LOG.trace("executing '{}'", command);
pis = new PipedInputStream(output);
input = new DataInputStream(pis);
parser = new PsParser(input, "opennms_bootstrap.jar", "status", 0);
parser.start();
executor.setStreamHandler(streamHandler);
int exitValue = executor.execute(command);
IOUtils.closeQuietly(output);
parser.join(MAX_PROCESS_WAIT);
processes.addAll(parser.getProcesses());
LOG.trace("finished '{}'", command);
if (exitValue != 0) {
LOG.debug("error running '{}': exit value was {}", command, exitValue);
}
} catch (final Exception e) {
LOG.debug("error running '{}'", command, e);
} finally {
IOUtils.closeQuietly(input);
IOUtils.closeQuietly(pis);
IOUtils.closeQuietly(output);
}
}
}
if (processes.size() == 0) {
LOG.warn("Unable to find any OpenNMS processes.");
}
return processes;
}
use of org.apache.commons.exec.ExecuteWatchdog in project project-build-plugin by axonivy.
the class EngineControl method start.
public Executor start() throws Exception {
CommandLine startCmd = toEngineCommand(Command.start);
context.log.info("Start Axon.ivy Engine in folder: " + context.engineDirectory);
Executor executor = createEngineExecutor();
executor.setStreamHandler(createEngineLogStreamForwarder(logLine -> findStartEngineUrl(logLine)));
executor.setWatchdog(new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT));
executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
executor.execute(startCmd, asynchExecutionHandler());
waitForEngineStart(executor);
return executor;
}
Aggregations