new HashSet<>(), StreamToLineProcessor.wrap(Processors.silent())); Processors.connect(new InputStreamReader(process.getInputStream()), logWatch); logWatch.waitForResult(2000); logger.trace("logWatch output: {}", logWatch.getOutput()); return logWatch.isInitWithSuccess();
public static boolean isProcessRunning(Platform platform, long pid) { try { final Process pidof; if (platform.isUnixLike()) { pidof = Runtime.getRuntime().exec( new String[] { "kill", "-0", "" + pid }); return pidof.waitFor() == 0; } else { // windows // process might be in either NOT RESPONDING due to // firewall blocking, or could be RUNNING final String[] cmd = { "tasklist.exe", "/FI", "PID eq " + pid ,"/FO", "CSV" }; logger.trace("Command: {}", Arrays.asList(cmd)); ProcessBuilder processBuilder = ProcessControl .newProcessBuilder(Arrays.asList(cmd), true); Process process = processBuilder.start(); // look for the PID in the output, pass it in for 'success' state LogWatchStreamProcessor logWatch = new LogWatchStreamProcessor(""+pid, new HashSet<String>(), StreamToLineProcessor.wrap(Processors.silent())); Processors.connect(new InputStreamReader(process.getInputStream()), logWatch); logWatch.waitForResult(2000); logger.trace("logWatch output: {}", logWatch.getOutput()); return logWatch.isInitWithSuccess(); } } catch (IOException | InterruptedException e) { logger.error("Trying to get process status", e); } return false; }
@Override protected final void onAfterProcessStart(ProcessControl process, IRuntimeConfig runtimeConfig) throws IOException { ProcessOutput outputConfig = runtimeConfig.getProcessOutput(); LogWatchStreamProcessor logWatch = new LogWatchStreamProcessor(successMessage(), knownFailureMessages(), StreamToLineProcessor.wrap(outputConfig.getOutput())); Processors.connect(process.getReader(), logWatch); Processors.connect(process.getError(), StreamToLineProcessor.wrap(outputConfig.getError())); logWatch.waitForResult(getConfig().timeout().getStartupTimeout()); if (logWatch.isInitWithSuccess()) { setProcessId(Mongod.getMongodProcessId(logWatch.getOutput(), -1)); } else { String failureFound = logWatch.getFailureFound(); if (failureFound==null) { failureFound="\n" + "----------------------\n" + "Hmm.. no failure message.. \n" + "...the cause must be somewhere in the process output\n" + "----------------------\n" + ""+logWatch.getOutput(); } try { // Process could be finished with success here! In this case no need to throw an exception! if(process.waitFor() != 0){ throw new IOException("Could not start process: "+failureFound); } } catch (InterruptedException e) { throw new IOException("Could not start process: "+failureFound, e); } } }
@Override protected final void onAfterProcessStart(ProcessControl process, IRuntimeConfig runtimeConfig) throws IOException { ProcessOutput outputConfig = runtimeConfig.getProcessOutput(); // memcached prints all status messages on error console, and only // if // started with -vv LogWatchStreamProcessor logWatch = new LogWatchStreamProcessor( "server listening", knownFailureMessages(), StreamToLineProcessor.wrap(outputConfig.getError())); Processors.connect(process.getError(), logWatch); logWatch.waitForResult(getConfig().timeout().getStartupTimeout()); if (!logWatch.isInitWithSuccess()) { throw new IOException("Could not start process:" + logWatch.getOutput()); } if (Distribution.detectFor(getConfig().version()).getPlatform() == Platform.Windows) { // On windows, process won't be writing a pid file. Sigh. Use // some // force to find out the pid anyways. This works pretty well // on Unix // (won't help us much here..) and needs Sigar lib on Windows. setProcessId(process.getPid()); } else { setProcessId(getPidFromFile(pidFile())); } }