List of usage examples for org.apache.commons.exec ExecuteWatchdog killedProcess
boolean killedProcess
To view the source code for org.apache.commons.exec ExecuteWatchdog killedProcess.
Click Source Link
From source file:com.datastax.driver.core.CCMBridge.java
private String execute(String command, Object... args) { String fullCommand = String.format(command, args) + " --config-dir=" + ccmDir; Closer closer = Closer.create();/*from w w w . ja v a 2s .c o m*/ // 10 minutes timeout ExecuteWatchdog watchDog = new ExecuteWatchdog(TimeUnit.MINUTES.toMillis(10)); StringWriter sw = new StringWriter(); final PrintWriter pw = new PrintWriter(sw); closer.register(pw); try { logger.trace("Executing: " + fullCommand); CommandLine cli = CommandLine.parse(fullCommand); Executor executor = new DefaultExecutor(); LogOutputStream outStream = new LogOutputStream() { @Override protected void processLine(String line, int logLevel) { String out = "ccmout> " + line; logger.debug(out); pw.println(out); } }; LogOutputStream errStream = new LogOutputStream() { @Override protected void processLine(String line, int logLevel) { String err = "ccmerr> " + line; logger.error(err); pw.println(err); } }; closer.register(outStream); closer.register(errStream); ExecuteStreamHandler streamHandler = new PumpStreamHandler(outStream, errStream); executor.setStreamHandler(streamHandler); executor.setWatchdog(watchDog); int retValue = executor.execute(cli, ENVIRONMENT_MAP); if (retValue != 0) { logger.error("Non-zero exit code ({}) returned from executing ccm command: {}", retValue, fullCommand); pw.flush(); throw new CCMException( String.format("Non-zero exit code (%s) returned from executing ccm command: %s", retValue, fullCommand), sw.toString()); } } catch (IOException e) { if (watchDog.killedProcess()) logger.error("The command {} was killed after 10 minutes", fullCommand); pw.flush(); throw new CCMException(String.format("The command %s failed to execute", fullCommand), sw.toString(), e); } finally { try { closer.close(); } catch (IOException e) { Throwables.propagate(e); } } return sw.toString(); }
From source file:org.apache.hive.hcatalog.templeton.StreamOutputWriter.java
private ExecBean auxRun(String program, List<String> args, Map<String, String> env) throws NotAuthorizedException, ExecuteException, IOException { DefaultExecutor executor = new DefaultExecutor(); executor.setExitValues(null);/*from w ww. j av a 2 s .co m*/ // 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; }
From source file:org.apache.zeppelin.submarine.job.thread.JobRunThread.java
public void run() { boolean tryLock = lockRunning.tryLock(); if (false == tryLock) { LOGGER.warn("Can not get JobRunThread lockRunning!"); return;// w w w .j av a2s. c o m } SubmarineUI submarineUI = submarineJob.getSubmarineUI(); try { InterpreterContext intpContext = submarineJob.getIntpContext(); String noteId = intpContext.getNoteId(); String userName = intpContext.getAuthenticationInfo().getUser(); String jobName = SubmarineUtils.getJobName(userName, noteId); if (true == running.get()) { String message = String.format("Job %s already running.", jobName); submarineUI.outputLog("WARN", message); LOGGER.warn(message); return; } running.set(true); Properties properties = submarineJob.getProperties(); HdfsClient hdfsClient = submarineJob.getHdfsClient(); File pythonWorkDir = submarineJob.getPythonWorkDir(); submarineJob.setCurrentJobState(EXECUTE_SUBMARINE); String algorithmPath = properties.getProperty(SubmarineConstants.SUBMARINE_ALGORITHM_HDFS_PATH, ""); if (!algorithmPath.startsWith("hdfs://")) { String message = "Algorithm file upload HDFS path, " + "Must be `hdfs://` prefix. now setting " + algorithmPath; submarineUI.outputLog("Configuration error", message); return; } List<ParagraphInfo> paragraphInfos = intpContext.getIntpEventClient().getParagraphList(userName, noteId); String outputMsg = hdfsClient.saveParagraphToFiles(noteId, paragraphInfos, pythonWorkDir == null ? "" : pythonWorkDir.getAbsolutePath(), properties); if (!StringUtils.isEmpty(outputMsg)) { submarineUI.outputLog("Save algorithm file", outputMsg); } HashMap jinjaParams = SubmarineUtils.propertiesToJinjaParams(properties, submarineJob, true); URL urlTemplate = Resources.getResource(SubmarineJob.SUBMARINE_JOBRUN_TF_JINJA); String template = Resources.toString(urlTemplate, Charsets.UTF_8); Jinjava jinjava = new Jinjava(); String submarineCmd = jinjava.render(template, jinjaParams); // If the first line is a newline, delete the newline int firstLineIsNewline = submarineCmd.indexOf("\n"); if (firstLineIsNewline == 0) { submarineCmd = submarineCmd.replaceFirst("\n", ""); } StringBuffer sbLogs = new StringBuffer(submarineCmd); submarineUI.outputLog("Submarine submit command", sbLogs.toString()); long timeout = Long .valueOf(properties.getProperty(SubmarineJob.TIMEOUT_PROPERTY, SubmarineJob.defaultTimeout)); CommandLine cmdLine = CommandLine.parse(SubmarineJob.shell); cmdLine.addArgument(submarineCmd, false); DefaultExecutor executor = new DefaultExecutor(); ExecuteWatchdog watchDog = new ExecuteWatchdog(timeout); executor.setWatchdog(watchDog); StringBuffer sbLogOutput = new StringBuffer(); executor.setStreamHandler(new PumpStreamHandler(new LogOutputStream() { @Override protected void processLine(String line, int level) { line = line.trim(); if (!StringUtils.isEmpty(line)) { sbLogOutput.append(line + "\n"); } } })); if (Boolean.valueOf(properties.getProperty(SubmarineJob.DIRECTORY_USER_HOME))) { executor.setWorkingDirectory(new File(System.getProperty("user.home"))); } Map<String, String> env = new HashMap<>(); String launchMode = (String) jinjaParams.get(SubmarineConstants.INTERPRETER_LAUNCH_MODE); if (StringUtils.equals(launchMode, "yarn")) { // Set environment variables in the submarine interpreter container run on yarn String javaHome, hadoopHome, hadoopConf; javaHome = (String) jinjaParams.get(SubmarineConstants.DOCKER_JAVA_HOME); hadoopHome = (String) jinjaParams.get(SubmarineConstants.DOCKER_HADOOP_HDFS_HOME); hadoopConf = (String) jinjaParams.get(SubmarineConstants.SUBMARINE_HADOOP_CONF_DIR); env.put("JAVA_HOME", javaHome); env.put("HADOOP_HOME", hadoopHome); env.put("HADOOP_HDFS_HOME", hadoopHome); env.put("HADOOP_CONF_DIR", hadoopConf); env.put("YARN_CONF_DIR", hadoopConf); env.put("CLASSPATH", "`$HADOOP_HDFS_HOME/bin/hadoop classpath --glob`"); env.put("ZEPPELIN_FORCE_STOP", "true"); } LOGGER.info("Execute EVN: {}, Command: {} ", env.toString(), submarineCmd); AtomicBoolean cmdLineRunning = new AtomicBoolean(true); executor.execute(cmdLine, env, new DefaultExecuteResultHandler() { @Override public void onProcessComplete(int exitValue) { String message = String.format("jobName %s ProcessComplete exit value is : %d", jobName, exitValue); LOGGER.info(message); submarineUI.outputLog("JOR RUN COMPLETE", message); cmdLineRunning.set(false); submarineJob.setCurrentJobState(EXECUTE_SUBMARINE_FINISHED); } @Override public void onProcessFailed(ExecuteException e) { String message = String.format("jobName %s ProcessFailed exit value is : %d, exception is : %s", jobName, e.getExitValue(), e.getMessage()); LOGGER.error(message); submarineUI.outputLog("JOR RUN FAILED", message); cmdLineRunning.set(false); submarineJob.setCurrentJobState(EXECUTE_SUBMARINE_ERROR); } }); int loopCount = 100; while ((loopCount-- > 0) && cmdLineRunning.get() && running.get()) { Thread.sleep(1000); } if (watchDog.isWatching()) { watchDog.destroyProcess(); Thread.sleep(1000); } if (watchDog.isWatching()) { watchDog.killedProcess(); } // Check if it has been submitted to YARN Map<String, Object> jobState = submarineJob.getJobStateByYarn(jobName); loopCount = 50; while ((loopCount-- > 0) && !jobState.containsKey("state") && running.get()) { Thread.sleep(3000); jobState = submarineJob.getJobStateByYarn(jobName); } if (!jobState.containsKey("state")) { String message = String.format("JOB %s was not submitted to YARN!", jobName); LOGGER.error(message); submarineUI.outputLog("JOR RUN FAILED", message); submarineJob.setCurrentJobState(EXECUTE_SUBMARINE_ERROR); } } catch (Exception e) { LOGGER.error(e.getMessage(), e); submarineJob.setCurrentJobState(EXECUTE_SUBMARINE_ERROR); submarineUI.outputLog("Exception", e.getMessage()); } finally { running.set(false); lockRunning.unlock(); } }
From source file:org.apache.zeppelin.submarine.job.thread.TensorboardRunThread.java
public void run() { SubmarineUI submarineUI = submarineJob.getSubmarineUI(); boolean tryLock = lockRunning.tryLock(); try {//from w ww . j a va2s . c om Properties properties = submarineJob.getProperties(); String tensorboardName = SubmarineUtils.getTensorboardName(submarineJob.getUserName()); if (true == running.get()) { String message = String.format("tensorboard %s already running.", tensorboardName); submarineUI.outputLog("WARN", message); LOGGER.warn(message); return; } running.set(true); HashMap jinjaParams = SubmarineUtils.propertiesToJinjaParams(properties, submarineJob, false); // update jobName -> tensorboardName jinjaParams.put(SubmarineConstants.JOB_NAME, tensorboardName); URL urlTemplate = Resources.getResource(SubmarineJob.SUBMARINE_TENSORBOARD_JINJA); String template = Resources.toString(urlTemplate, Charsets.UTF_8); Jinjava jinjava = new Jinjava(); String submarineCmd = jinjava.render(template, jinjaParams); // If the first line is a newline, delete the newline int firstLineIsNewline = submarineCmd.indexOf("\n"); if (firstLineIsNewline == 0) { submarineCmd = submarineCmd.replaceFirst("\n", ""); } StringBuffer sbLogs = new StringBuffer(submarineCmd); submarineUI.outputLog("Submarine submit command", sbLogs.toString()); long timeout = Long .valueOf(properties.getProperty(SubmarineJob.TIMEOUT_PROPERTY, SubmarineJob.defaultTimeout)); CommandLine cmdLine = CommandLine.parse(SubmarineJob.shell); cmdLine.addArgument(submarineCmd, false); DefaultExecutor executor = new DefaultExecutor(); ExecuteWatchdog watchDog = new ExecuteWatchdog(timeout); executor.setWatchdog(watchDog); StringBuffer sbLogOutput = new StringBuffer(); executor.setStreamHandler(new PumpStreamHandler(new LogOutputStream() { @Override protected void processLine(String line, int level) { line = line.trim(); if (!StringUtils.isEmpty(line)) { sbLogOutput.append(line + "\n"); } } })); if (Boolean.valueOf(properties.getProperty(SubmarineJob.DIRECTORY_USER_HOME))) { executor.setWorkingDirectory(new File(System.getProperty("user.home"))); } Map<String, String> env = new HashMap<>(); String launchMode = (String) jinjaParams.get(SubmarineConstants.INTERPRETER_LAUNCH_MODE); if (StringUtils.equals(launchMode, "yarn")) { // Set environment variables in the container String javaHome, hadoopHome, hadoopConf; javaHome = (String) jinjaParams.get(SubmarineConstants.DOCKER_JAVA_HOME); hadoopHome = (String) jinjaParams.get(SubmarineConstants.DOCKER_HADOOP_HDFS_HOME); hadoopConf = (String) jinjaParams.get(SubmarineConstants.SUBMARINE_HADOOP_CONF_DIR); env.put("JAVA_HOME", javaHome); env.put("HADOOP_HOME", hadoopHome); env.put("HADOOP_HDFS_HOME", hadoopHome); env.put("HADOOP_CONF_DIR", hadoopConf); env.put("YARN_CONF_DIR", hadoopConf); env.put("CLASSPATH", "`$HADOOP_HDFS_HOME/bin/hadoop classpath --glob`"); } LOGGER.info("Execute EVN: {}, Command: {} ", env.toString(), submarineCmd); AtomicBoolean cmdLineRunning = new AtomicBoolean(true); executor.execute(cmdLine, env, new DefaultExecuteResultHandler() { @Override public void onProcessComplete(int exitValue) { String message = String.format("jobName %s ProcessComplete exit value is : %d", tensorboardName, exitValue); LOGGER.info(message); submarineUI.outputLog("TENSORBOARD RUN COMPLETE", message); cmdLineRunning.set(false); } @Override public void onProcessFailed(ExecuteException e) { String message = String.format("jobName %s ProcessFailed exit value is : %d, exception is : %s", tensorboardName, e.getExitValue(), e.getMessage()); LOGGER.error(message); submarineUI.outputLog("TENSORBOARD RUN FAILED", message); cmdLineRunning.set(false); } }); int loopCount = 100; while ((loopCount-- > 0) && cmdLineRunning.get() && running.get()) { Thread.sleep(1000); } if (watchDog.isWatching()) { watchDog.destroyProcess(); Thread.sleep(1000); } if (watchDog.isWatching()) { watchDog.killedProcess(); } // Check if it has been submitted to YARN Map<String, Object> jobState = submarineJob.getJobStateByYarn(tensorboardName); loopCount = 50; while ((loopCount-- > 0) && !jobState.containsKey("state") && running.get()) { Thread.sleep(3000); jobState = submarineJob.getJobStateByYarn(tensorboardName); } if (!jobState.containsKey("state")) { String message = String.format("tensorboard %s was not submitted to YARN!", tensorboardName); LOGGER.error(message); submarineUI.outputLog("JOR RUN FAILED", message); } } catch (Exception e) { LOGGER.error(e.getMessage(), e); submarineUI.outputLog("Exception", e.getMessage()); } finally { running.set(false); lockRunning.unlock(); } }
From source file:org.cloudifysource.dsl.context.utils.VolumeUtils.java
private static void executeCommandLine(final String commandLine, final long timeout) throws LocalStorageOperationException, TimeoutException { Executor executor = new DefaultExecutor(); executor.setExitValue(0);// w w w .j a v a2 s.c o m ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout); executor.setWatchdog(watchdog); ProcessOutputStream outAndErr = new ProcessOutputStream(); try { PumpStreamHandler streamHandler = new PumpStreamHandler(outAndErr); executor.setStreamHandler(streamHandler); logger.info("Executing commandLine : '" + commandLine + "'"); executor.execute(CommandLine.parse(commandLine)); logger.info("Execution completed successfully. Process output was : " + outAndErr.getOutput()); } catch (final Exception e) { if (watchdog.killedProcess()) { throw new TimeoutException("Timed out while executing commandLine : '" + commandLine + "'"); } throw new LocalStorageOperationException("Failed executing commandLine : '" + commandLine + ". Process output was : " + outAndErr.getOutput(), e); } }
From source file:org.cloudifysource.utilitydomain.context.blockstorage.VolumeUtils.java
private static void executeCommandLine(final String commandLine, final long timeout) throws LocalStorageOperationException, TimeoutException { Executor executor = new DefaultExecutor(); executor.setExitValue(0);//from w w w . jav a 2s .c o m ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout); executor.setWatchdog(watchdog); ProcessOutputStream outAndErr = new ProcessOutputStream(); try { PumpStreamHandler streamHandler = new PumpStreamHandler(outAndErr); executor.setStreamHandler(streamHandler); logger.info("Executing commandLine : '" + commandLine + "'"); executor.execute(CommandLine.parse(commandLine)); logger.info("Execution completed successfully. Process output was : " + outAndErr.getOutput()); } catch (final Exception e) { if (watchdog.killedProcess()) { throw new TimeoutException("Timed out while executing commandLine : '" + commandLine + "'"); } throw new LocalStorageOperationException("Failed executing commandLine : '" + commandLine + ". Process output was : " + outAndErr.getOutput(), e); } }
From source file:org.cloudifysource.utilitydomain.context.blockstorage.VolumeUtils.java
private static String executeSilentCommandLineReturnOutput(final String commandLine, final long timeout) throws LocalStorageOperationException, TimeoutException { Executor executor = new DefaultExecutor(); executor.setExitValue(0);//from w w w. jav a2 s . c o m ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout); executor.setWatchdog(watchdog); ProcessOutputStream outAndErr = new ProcessOutputStream(); try { PumpStreamHandler streamHandler = new PumpStreamHandler(outAndErr); executor.setStreamHandler(streamHandler); executor.execute(CommandLine.parse(commandLine)); } catch (final Exception e) { if (watchdog.killedProcess()) { throw new TimeoutException("Timed out while executing commandLine : '" + commandLine + "'"); } throw new LocalStorageOperationException("Failed executing commandLine : '" + commandLine + ". Process output was : " + outAndErr.getOutput(), e); } return outAndErr.getOutput(); }
From source file:org.eclipse.sisu.equinox.launching.internal.DefaultEquinoxLauncher.java
@Override public int execute(LaunchConfiguration configuration, int forkedProcessTimeoutInSeconds) throws EquinoxLaunchingException { String executable = configuration.getJvmExecutable(); if (executable == null || "".equals(executable)) { // use the same JVM as the one used to run Maven (the "java.home" one) executable = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java"; if (File.separatorChar == '\\') { executable = executable + ".exe"; }//from w ww .java 2s. co m } CommandLine cli = new CommandLine(executable); final boolean handleQuotes = false; cli.addArguments(configuration.getVMArguments(), handleQuotes); cli.addArguments(new String[] { "-jar", getCanonicalPath(configuration.getLauncherJar()) }, handleQuotes); cli.addArguments(configuration.getProgramArguments(), handleQuotes); log.info("Command line:\n\t" + cli.toString()); DefaultExecutor executor = new DefaultExecutor(); ExecuteWatchdog watchdog = null; if (forkedProcessTimeoutInSeconds > 0) { watchdog = new ExecuteWatchdog(forkedProcessTimeoutInSeconds * 1000L); executor.setWatchdog(watchdog); } // best effort to avoid orphaned child process executor.setProcessDestroyer(new ShutdownHookProcessDestroyer()); executor.setWorkingDirectory(configuration.getWorkingDirectory()); try { return executor.execute(cli, getMergedEnvironment(configuration)); } catch (ExecuteException e) { if (watchdog != null && watchdog.killedProcess()) { log.error("Timeout " + forkedProcessTimeoutInSeconds + " s exceeded. Process was killed."); } return e.getExitValue(); } catch (IOException e) { throw new EquinoxLaunchingException(e); } }
From source file:org.epics.archiverappliance.TomcatSetup.java
public void tearDown() throws Exception { for (ExecuteWatchdog watchdog : executorWatchDogs) { // We brutally kill the process watchdog.destroyProcess();/* w w w. j av a 2 s . co m*/ try { Thread.sleep(10 * 1000); } catch (Exception ex) { } assert (watchdog.killedProcess()); } for (File cleanupFolder : cleanupFolders) { logger.debug("Cleaning up folder " + cleanupFolder.getAbsolutePath()); FileUtils.deleteDirectory(cleanupFolder); } // So many timeouts; if we are running multiple tests in sequence; the Tomcat socket seems to wind up in a TIME_WAIT site for about 2 minutes. // Setting the net.netfilter.nf_conntrack_tcp_timeout_time_wait (using sysctl) seems to hurt other behaviours... // Sigh! try { Thread.sleep(3 * 60 * 1000); } catch (Exception ex) { } }
From source file:org.evosuite.utils.ProcessLauncher.java
public int launchNewProcess(File baseDir, String cmdString, int timeout) throws IOException, ProcessTimeoutException { DefaultExecutor executor = new DefaultExecutor(); ExecuteWatchdog timeoutWatchdog = new ExecuteWatchdog(timeout); executor.setWatchdog(timeoutWatchdog); PumpStreamHandler streamHandler = new PumpStreamHandler(this.outAndErr, this.outAndErr, this.input); executor.setStreamHandler(streamHandler); if (baseDir != null) { executor.setWorkingDirectory(baseDir); }//from ww w .j a va 2 s .co m int exitValue; try { logger.debug("About to execute command " + cmdString); exitValue = executor.execute(CommandLine.parse(cmdString)); if (executor.isFailure(exitValue) && timeoutWatchdog.killedProcess()) { // it was killed on purpose by the watchdog logger.debug("A timeout occured while executing a process"); logger.debug("The command is " + cmdString); throw new ProcessTimeoutException("A timeout occurred while executing command " + cmdString); } return exitValue; } catch (ExecuteException e) { if (timeoutWatchdog.killedProcess()) { logger.debug("A timeout occured while executing a process"); logger.debug("The command is " + cmdString); throw new ProcessTimeoutException("A timeout occurred while executing command " + cmdString); } else { throw e; } } }