List of usage examples for java.lang ProcessBuilder redirectErrorStream
boolean redirectErrorStream
To view the source code for java.lang ProcessBuilder redirectErrorStream.
Click Source Link
From source file:org.apache.geode.internal.cache.IncrementalBackupDUnitTest.java
/** * Executes a shell command in an external process. * //from w w w .j a v a 2s . c o m * @param command a shell command. * @return the exit value of processing the shell command. */ private int execute(String command) throws IOException, InterruptedException { final ProcessBuilder builder = new ProcessBuilder(command); builder.redirectErrorStream(true); final Process process = builder.start(); /* * Consume standard out. */ new Thread(new Runnable() { @Override public void run() { try { BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; do { line = reader.readLine(); log(line); } while (null != line); reader.close(); } catch (IOException e) { log("Execute: error while reading standard in: " + e.getMessage()); } } }).start(); /* * Consume standard error. */ new Thread(new Runnable() { @Override public void run() { try { BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream())); String line; do { line = reader.readLine(); } while (null != line); reader.close(); } catch (IOException e) { log("Execute: error while reading standard error: " + e.getMessage()); } } }).start(); return process.waitFor(); }
From source file:org.cloudifysource.usm.launcher.DefaultProcessLauncher.java
private Process launch(final List<String> commandLineParams, final File workingDir, final int retries, final boolean redirectErrorStream, final File outputFile, final File errorFile, final LifecycleEvents event) throws USMException { if (outputFile == null && errorFile != null || outputFile != null && errorFile == null) { throw new IllegalArgumentException("Both output and error files must be set, or none of them"); }/*from ww w. ja va 2s .c om*/ if (redirectErrorStream && (outputFile != null || errorFile != null)) { throw new IllegalArgumentException( "If redirectError option is chosen, neither output file or error file can be set"); } List<String> modifiedCommandLineParams = null; modifiedCommandLineParams = commandLineParams; if (isDebugEvent(event)) { // create environment for debugging the event and modify the command line. modifyCommandLine(modifiedCommandLineParams, workingDir, outputFile, errorFile, event); logger.info("DEBUG BREAKPOINT!"); DebugHookInvoker dhi = new DebugHookInvoker(); final ClassLoader loader = this.configutaion.getDslClassLoader(); logger.info("DSL Class Loader is: " + loader); modifiedCommandLineParams = dhi.setUpDebugHook(this.configutaion.getServiceContext(), modifiedCommandLineParams, loader, this.debugMode); } else { modifyCommandLine(modifiedCommandLineParams, workingDir, outputFile, errorFile, event); } // JDK7 on Windows Specific modifications // See CLOUDIFY-1787 for more details. File tempBatchFile = null; if (shouldModifyCommandLineForJDK7(outputFile)) { tempBatchFile = createTempFileForJDK7(workingDir); modifyCommandLineForJDK7ProcessBuilder(modifiedCommandLineParams, outputFile, workingDir, tempBatchFile); } final String modifiedCommandLine = org.springframework.util.StringUtils .collectionToDelimitedString(modifiedCommandLineParams, " "); this.commandLine = modifiedCommandLineParams; int attempt = 1; USMException ex = null; while (attempt <= retries + 1) { final ProcessBuilder pb = new ProcessBuilder(modifiedCommandLineParams); pb.directory(workingDir); pb.redirectErrorStream(redirectErrorStream); final Map<String, String> env = createEnvironment(); pb.environment().putAll(env); try { logger.fine("Parsed command line: " + commandLineParams.toString()); final String fileInitialMessage = "Starting service process in working directory:'" + workingDir + "' " + "at:'" + new Date() + "' with command:'" + modifiedCommandLineParams + "'" + System.getProperty("line.separator"); if (outputFile != null) { appendMessageToFile(fileInitialMessage, outputFile); } if (errorFile != null) { appendMessageToFile(fileInitialMessage, errorFile); } return pb.start(); } catch (final IOException e) { ex = new USMException("Failed to start process with command line: " + modifiedCommandLine, e); logger.log(Level.SEVERE, "Process start attempt number " + attempt + " failed", ex); } ++attempt; } throw ex; }
From source file:org.apache.hadoop.util.Shell.java
/** Run a command */ private void runCommand() throws IOException { ProcessBuilder builder = new ProcessBuilder(getExecString()); Timer timeOutTimer = null;//from w w w .ja v a 2s .c o m ShellTimeoutTimerTask timeoutTimerTask = null; timedOut = new AtomicBoolean(false); completed = new AtomicBoolean(false); if (environment != null) { builder.environment().putAll(this.environment); } if (dir != null) { builder.directory(this.dir); } builder.redirectErrorStream(redirectErrorStream); if (Shell.WINDOWS) { synchronized (WindowsProcessLaunchLock) { // To workaround the race condition issue with child processes // inheriting unintended handles during process launch that can // lead to hangs on reading output and error streams, we // serialize process creation. More info available at: // http://support.microsoft.com/kb/315939 process = builder.start(); } } else { process = builder.start(); } if (timeOutInterval > 0) { timeOutTimer = new Timer("Shell command timeout"); timeoutTimerTask = new ShellTimeoutTimerTask(this); //One time scheduling. timeOutTimer.schedule(timeoutTimerTask, timeOutInterval); } final BufferedReader errReader = new BufferedReader(new InputStreamReader(process.getErrorStream())); BufferedReader inReader = new BufferedReader(new InputStreamReader(process.getInputStream())); final StringBuffer errMsg = new StringBuffer(); // read error and input streams as this would free up the buffers // free the error stream buffer Thread errThread = new Thread() { @Override public void run() { try { String line = errReader.readLine(); while ((line != null) && !isInterrupted()) { errMsg.append(line); errMsg.append(System.getProperty("line.separator")); line = errReader.readLine(); } } catch (IOException ioe) { LOG.warn("Error reading the error stream", ioe); } } }; try { errThread.start(); } catch (IllegalStateException ise) { } try { parseExecResult(inReader); // parse the output // clear the input stream buffer String line = inReader.readLine(); while (line != null) { line = inReader.readLine(); } // wait for the process to finish and check the exit code exitCode = process.waitFor(); try { // make sure that the error thread exits errThread.join(); } catch (InterruptedException ie) { LOG.warn("Interrupted while reading the error stream", ie); } completed.set(true); //the timeout thread handling //taken care in finally block if (exitCode != 0) { throw new ExitCodeException(exitCode, errMsg.toString()); } } catch (InterruptedException ie) { throw new IOException(ie.toString()); } finally { if (timeOutTimer != null) { timeOutTimer.cancel(); } // close the input stream try { // JDK 7 tries to automatically drain the input streams for us // when the process exits, but since close is not synchronized, // it creates a race if we close the stream first and the same // fd is recycled. the stream draining thread will attempt to // drain that fd!! it may block, OOM, or cause bizarre behavior // see: https://bugs.openjdk.java.net/browse/JDK-8024521 // issue is fixed in build 7u60 InputStream stdout = process.getInputStream(); synchronized (stdout) { inReader.close(); } } catch (IOException ioe) { LOG.warn("Error while closing the input stream", ioe); } try { if (!completed.get()) { errThread.interrupt(); errThread.join(); } } catch (InterruptedException ie) { LOG.warn("Interrupted while joining errThread"); } try { InputStream stderr = process.getErrorStream(); synchronized (stderr) { errReader.close(); } } catch (IOException ioe) { LOG.warn("Error while closing the error stream", ioe); } process.destroy(); lastTime = Time.now(); } }
From source file:org.apache.htrace.impl.HTracedProcess.java
private HTracedProcess(Builder builder) throws Exception { this.htracedPath = Paths.get("target", "..", "go", "build", "htraced").toFile(); if (!this.htracedPath.exists()) { throw new RuntimeException("No htraced binary exists at " + this.htracedPath); }//from ww w . jav a 2 s. c om this.dataDir = new DataDir(); // Create a notifier socket bound to a random port. ServerSocket listener = new ServerSocket(0); boolean success = false; Process process = null; HttpClient http = null; try { // Use a random port for the web address. No 'scheme' yet. String random = builder.host + ":0"; String logPath = new File(dataDir.get(), "log.txt").getAbsolutePath(); // Pass cmdline args to htraced to it uses our test dir for data. ProcessBuilder pb = new ProcessBuilder(htracedPath.getAbsolutePath(), "-Dlog.level=TRACE", "-Dlog.path=" + logPath, "-Dweb.address=" + random, "-Dhrpc.address=" + random, "-Ddata.store.clear=true", "-Dstartup.notification.address=localhost:" + listener.getLocalPort(), "-Ddata.store.directories=" + dataDir.get().getAbsolutePath()); // Set HTRACED_CONF_DIR to the temporary directory we just created, to // ensure that it doesn't pull in any other configuration file that might // be on this test machine. Map<String, String> env = pb.environment(); env.put("HTRACED_CONF_DIR", dataDir.get().getAbsolutePath()); // Remove any HTRACED_WEB_DIR variable that might be set, to ensure that // we use the default value (which finds the local web files by relative // path). env.remove("HTRACED_WEB_DIR"); pb.redirectErrorStream(true); // Inherit STDERR/STDOUT i/o; dumps on console for now. Can add logs later. pb.inheritIO(); pb.directory(dataDir.get()); //assert pb.redirectInput() == Redirect.PIPE; //assert pb.redirectOutput().file() == dataDir; process = pb.start(); assert process.getInputStream().read() == -1; StartupNotificationData data = readStartupNotification(listener); httpAddr = data.httpAddr; hrpcAddr = data.hrpcAddr; LOG.info("Started htraced process " + data.processId + " with http " + "address " + data.httpAddr + ", logging to " + logPath); http = RestBufferManager.createHttpClient(60000L, 60000L); http.start(); success = true; } finally { if (!success) { // Clean up after failure if (process != null) { process.destroy(); process = null; } if (http != null) { http.stop(); } } delegate = process; listener.close(); httpClient = http; } }
From source file:com.buaa.cfs.utils.Shell.java
/** Run a command */ private void runCommand() throws IOException { ProcessBuilder builder = new ProcessBuilder(getExecString()); Timer timeOutTimer = null;/*from www .j ava2 s .co m*/ ShellTimeoutTimerTask timeoutTimerTask = null; timedOut = new AtomicBoolean(false); completed = new AtomicBoolean(false); if (environment != null) { builder.environment().putAll(this.environment); } if (dir != null) { builder.directory(this.dir); } builder.redirectErrorStream(redirectErrorStream); if (Shell.WINDOWS) { synchronized (WindowsProcessLaunchLock) { // To workaround the race condition issue with child processes // inheriting unintended handles during process launch that can // lead to hangs on reading output and error streams, we // serialize process creation. More info available at: // http://support.microsoft.com/kb/315939 process = builder.start(); } } else { process = builder.start(); } if (timeOutInterval > 0) { timeOutTimer = new Timer("Shell command timeout"); timeoutTimerTask = new ShellTimeoutTimerTask(this); //One time scheduling. timeOutTimer.schedule(timeoutTimerTask, timeOutInterval); } final BufferedReader errReader = new BufferedReader( new InputStreamReader(process.getErrorStream(), Charset.defaultCharset())); BufferedReader inReader = new BufferedReader( new InputStreamReader(process.getInputStream(), Charset.defaultCharset())); final StringBuffer errMsg = new StringBuffer(); // read error and input streams as this would free up the buffers // free the error stream buffer Thread errThread = new Thread() { @Override public void run() { try { String line = errReader.readLine(); while ((line != null) && !isInterrupted()) { errMsg.append(line); errMsg.append(System.getProperty("line.separator")); line = errReader.readLine(); } } catch (IOException ioe) { LOG.warn("Error reading the error stream", ioe); } } }; try { errThread.start(); } catch (IllegalStateException ise) { } catch (OutOfMemoryError oe) { LOG.error("Caught " + oe + ". One possible reason is that ulimit" + " setting of 'max user processes' is too low. If so, do" + " 'ulimit -u <largerNum>' and try again."); throw oe; } try { parseExecResult(inReader); // parse the output // clear the input stream buffer String line = inReader.readLine(); while (line != null) { line = inReader.readLine(); } // wait for the process to finish and check the exit code exitCode = process.waitFor(); // make sure that the error thread exits joinThread(errThread); completed.set(true); //the timeout thread handling //taken care in finally block if (exitCode != 0) { throw new ExitCodeException(exitCode, errMsg.toString()); } } catch (InterruptedException ie) { throw new IOException(ie.toString()); } finally { if (timeOutTimer != null) { timeOutTimer.cancel(); } // close the input stream try { // JDK 7 tries to automatically drain the input streams for us // when the process exits, but since close is not synchronized, // it creates a race if we close the stream first and the same // fd is recycled. the stream draining thread will attempt to // drain that fd!! it may block, OOM, or cause bizarre behavior // see: https://bugs.openjdk.java.net/browse/JDK-8024521 // issue is fixed in build 7u60 InputStream stdout = process.getInputStream(); synchronized (stdout) { inReader.close(); } } catch (IOException ioe) { LOG.warn("Error while closing the input stream", ioe); } if (!completed.get()) { errThread.interrupt(); joinThread(errThread); } try { InputStream stderr = process.getErrorStream(); synchronized (stderr) { errReader.close(); } } catch (IOException ioe) { LOG.warn("Error while closing the error stream", ioe); } process.destroy(); lastTime = Time.monotonicNow(); } }
From source file:org.kepler.ddp.director.DDPEngine.java
/** Check if the DDP engine server is running. If not, try to start it. * @param socketAddress Host and port of the server to check. * @param startScriptStr The script to start the server if not running. * @return True if a server was started, false if could connect to already running server. *//*w ww . ja v a 2 s. c om*/ protected boolean _checkServer(InetSocketAddress socketAddress, String startScriptStr) throws IllegalActionException { boolean startedServer = false; synchronized (_serverStartStopLock) { Socket socket = null; try { socket = new Socket(); boolean connected = false; try { socket.connect(socketAddress, _CONNECT_TIMEOUT); connected = true; } catch (IOException e) { System.out.println(_engineName + " server " + socketAddress + " does not appear to be running. Starting..."); // start the server if (!_checkFilesBeforeStartingServer()) { throw new IllegalActionException(_director, "One or more files required to start the server were not found."); } // see if the script is executable. kepler modules are zipped, // which does not preserve the permissions. File startScriptFile = new File(startScriptStr); if (!startScriptFile.canExecute()) { throw new IllegalActionException(_director, "The script " + startScriptFile + " is not executable.\n" + "You must change the permissions so that " + startScriptFile.getName() + " and all the other scripts in \n" + startScriptFile.getParent() + " are executable."); } ProcessBuilder builder = new ProcessBuilder(startScriptStr); // make sure JAVA_HOME is set java.util.Map<String, String> env = builder.environment(); if (env.get("JAVA_HOME") == null) { env.put("JAVA_HOME", System.getProperty("java.home")); } builder.redirectErrorStream(true); try { Process process = builder.start(); InetSocketAddress newAddress = _parseOutputFromStartingServer(process.getInputStream()); if (newAddress != null) { socketAddress = newAddress; } process.waitFor(); startedServer = true; } catch (Exception e1) { throw new IllegalActionException(_director, e1, "Unable to start " + _engineName + " server."); } int tries = 0; while (tries < 5) { // wait for the server to start try { Thread.sleep(5000); tries++; System.out.print("Connecting to " + _engineName + " server port try #" + tries + ": "); try { socket.close(); socket = new Socket(); socket.connect(socketAddress, _CONNECT_TIMEOUT); connected = true; System.out.println("connected."); break; } catch (IOException e1) { // do nothing System.out.println(e1); } } catch (InterruptedException e2) { throw new IllegalActionException(_director, e2, "Error while sleeping."); } } // if we get here, we were able to connect to the master/job manager port. // however, the server may not be completely initialized, so wait a few more seconds System.out.println("Waiting 15 seconds for " + _engineName + " server to initialize."); try { Thread.sleep(15000); } catch (InterruptedException e2) { throw new IllegalActionException(_director, e2, "Error while waiting " + " for " + _engineName + " server to initialize."); } } if (connected) { try { socket.close(); socket = null; } catch (IOException e) { throw new IllegalActionException(_director, e, "Error closing socket."); } } else { throw new IllegalActionException(_director, "Could not connect to " + _engineName + " server: " + socketAddress); } } finally { if (socket != null) { try { socket.close(); } catch (IOException e) { throw new IllegalActionException(_director, e, "Error closing socket."); } } } } return startedServer; }
From source file:org.apache.nifi.processors.standard.ExecuteProcess.java
protected Future<?> launchProcess(final ProcessContext context, final List<String> commandStrings, final Long batchNanos, final ProxyOutputStream proxyOut) throws IOException { final Boolean redirectErrorStream = context.getProperty(REDIRECT_ERROR_STREAM).asBoolean(); final ProcessBuilder builder = new ProcessBuilder(commandStrings); final String workingDirName = context.getProperty(WORKING_DIR).getValue(); if (workingDirName != null) { builder.directory(new File(workingDirName)); }/*from w w w.j a v a2 s.c o m*/ final Map<String, String> environment = new HashMap<>(); for (final Map.Entry<PropertyDescriptor, String> entry : context.getProperties().entrySet()) { if (entry.getKey().isDynamic()) { environment.put(entry.getKey().getName(), entry.getValue()); } } if (!environment.isEmpty()) { builder.environment().putAll(environment); } getLogger().info("Start creating new Process > {} ", new Object[] { commandStrings }); this.externalProcess = builder.redirectErrorStream(redirectErrorStream).start(); // Submit task to read error stream from process if (!redirectErrorStream) { executor.submit(new Runnable() { @Override public void run() { try (final BufferedReader reader = new BufferedReader( new InputStreamReader(externalProcess.getErrorStream()))) { reader.lines().filter(line -> line != null && line.length() > 0).forEach(getLogger()::warn); } catch (final IOException ioe) { } } }); } // Submit task to read output of Process and write to FlowFile. failure = new AtomicBoolean(false); final Future<?> future = executor.submit(new Callable<Object>() { @Override public Object call() throws IOException { try { if (batchNanos == null) { // if we aren't batching, just copy the stream from the // process to the flowfile. try (final BufferedInputStream bufferedIn = new BufferedInputStream( externalProcess.getInputStream())) { final byte[] buffer = new byte[4096]; int len; while ((len = bufferedIn.read(buffer)) > 0) { // NB!!!! Maybe all data should be read from // input stream in case of !isScheduled() to // avoid subprocess deadlock? // (we just don't write data to proxyOut) // Or because we don't use this subprocess // anymore anyway, we don't care? if (!isScheduled()) { return null; } proxyOut.write(buffer, 0, len); } } } else { // we are batching, which means that the output of the // process is text. It doesn't make sense to grab // arbitrary batches of bytes from some process and send // it along as a piece of data, so we assume that // setting a batch during means text. // Also, we don't want that text to get split up in the // middle of a line, so we use BufferedReader // to read lines of text and write them as lines of text. try (final BufferedReader reader = new BufferedReader( new InputStreamReader(externalProcess.getInputStream()))) { String line; while ((line = reader.readLine()) != null) { if (!isScheduled()) { return null; } proxyOut.write((line + "\n").getBytes(StandardCharsets.UTF_8)); } } } } catch (final IOException ioe) { failure.set(true); throw ioe; } finally { try { // Since we are going to exit anyway, one sec gives it an extra chance to exit gracefully. // In the future consider exposing it via configuration. boolean terminated = externalProcess.waitFor(1000, TimeUnit.MILLISECONDS); int exitCode = terminated ? externalProcess.exitValue() : -9999; getLogger().info("Process finished with exit code {} ", new Object[] { exitCode }); } catch (InterruptedException e1) { Thread.currentThread().interrupt(); } } return null; } }); return future; }