List of usage examples for java.lang ProcessBuilder environment
Map environment
To view the source code for java.lang ProcessBuilder environment.
Click Source Link
From source file:org.lab41.graphlab.twill.GraphLabRunnable.java
/** * GraphLab requires all the hadoop path globs to be expanded. * @return the classpath./* w ww. j av a 2 s .co m*/ * * @throws IOException * @throws InterruptedException * @throws ExecutionException */ private String getHadoopClassPath() throws IOException, InterruptedException, ExecutionException { List<String> args = Lists.newArrayList(); String hadoopCommonHome = System.getenv("HADOOP_COMMON_HOME"); if (hadoopCommonHome == null) { args.add("hadoop"); } else { args.add(hadoopCommonHome + "/bin/hadoop"); } args.add("classpath"); ProcessBuilder processBuilder = new ProcessBuilder(args); Map<String, String> env = processBuilder.environment(); // Inside a yarn application, HADOOP_CONF_DIR points at a path specific to the node manager and is not // intended to be used by other programs. env.remove("HADOOP_CONF_DIR"); String hadoopClientConfDir = env.get("HADOOP_CLIENT_CONF_DIR"); if (hadoopClientConfDir != null) { env.put("HADOOP_CONF_DIR", hadoopClientConfDir); } Process process = processBuilder.start(); StringWriter writer = new StringWriter(); IOUtils.copy(process.getInputStream(), writer, Charsets.US_ASCII); ExecutorService executor = Executors.newFixedThreadPool(2); try { Future<Void> errFuture = executor.submit(logInputStream(process.getErrorStream())); process.waitFor(); errFuture.get(); } finally { executor.shutdown(); } String classPath = writer.toString(); // Sometimes the classpath includes globs. List<String> classPathList = Lists.newArrayList(); for (String pattern : classPath.split(File.pathSeparator)) { LOG.debug("classpath pattern: " + pattern); File file = new File(pattern); File dir = file.getParentFile(); if (dir == null) { // We must be a top-level path, so just carry it through to the classpath. classPathList.add(file.toString()); } else { String[] children = dir.list(new WildcardFileFilter(file.getName())); if (children != null) { for (String path : children) { String f = new File(dir, path).toString(); LOG.debug("discovered jar: " + f); classPathList.add(f); } } } } return Joiner.on(File.pathSeparator).join(classPathList); }
From source file:org.nuxeo.ecm.platform.commandline.executor.service.executors.ShellExecutor.java
protected ExecResult exec1(CommandLineDescriptor cmdDesc, CmdParameters params, EnvironmentDescriptor env) throws IOException { // split the configured parameters while keeping quoted parts intact List<String> list = new ArrayList<>(); list.add(cmdDesc.getCommand());/* w w w .ja va 2 s .c o m*/ Matcher m = COMMAND_SPLIT.matcher(cmdDesc.getParametersString()); while (m.find()) { String word; if (m.group(1) != null) { word = m.group(1); // double-quoted } else if (m.group(2) != null) { word = m.group(2); // single-quoted } else { word = m.group(); // word } List<String> words = replaceParams(word, params); list.addAll(words); } List<Process> processes = new LinkedList<>(); List<Thread> pipes = new LinkedList<>(); List<String> command = new LinkedList<>(); Process process = null; for (Iterator<String> it = list.iterator(); it.hasNext();) { String word = it.next(); boolean build; if (word.equals("|")) { build = true; } else { // on Windows, look up the command in the PATH first if (command.isEmpty() && SystemUtils.IS_OS_WINDOWS) { command.add(getCommandAbsolutePath(word)); } else { command.add(word); } build = !it.hasNext(); } if (!build) { continue; } ProcessBuilder processBuilder = new ProcessBuilder(command); command = new LinkedList<>(); // reset for next loop processBuilder.directory(new File(env.getWorkingDirectory())); processBuilder.environment().putAll(env.getParameters()); processBuilder.redirectErrorStream(true); Process newProcess = processBuilder.start(); processes.add(newProcess); if (process == null) { // first process, nothing to input IOUtils.closeQuietly(newProcess.getOutputStream()); } else { // pipe previous process output into new process input // needs a thread doing the piping because Java has no way to connect two children processes directly // except through a filesystem named pipe but that can't be created in a portable manner Thread pipe = pipe(process.getInputStream(), newProcess.getOutputStream()); pipes.add(pipe); } process = newProcess; } // get result from last process @SuppressWarnings("null") BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; List<String> output = new ArrayList<>(); while ((line = reader.readLine()) != null) { output.add(line); } reader.close(); // wait for all processes, get first non-0 exit status int returnCode = 0; for (Process p : processes) { try { int exitCode = p.waitFor(); if (returnCode == 0) { returnCode = exitCode; } } catch (InterruptedException e) { ExceptionUtils.checkInterrupt(e); } } // wait for all pipes for (Thread t : pipes) { try { t.join(); } catch (InterruptedException e) { ExceptionUtils.checkInterrupt(e); } } return new ExecResult(null, output, 0, returnCode); }
From source file:com.streamsets.datacollector.util.SystemProcessImpl.java
@Override public void start(Map<String, String> env) throws IOException { Utils.checkState(output.createNewFile(), Utils.formatL("Could not create output file: {}", output)); Utils.checkState(error.createNewFile(), Utils.formatL("Could not create error file: {}", error)); Utils.checkState(delegate == null, "start can only be called once"); LOG.info("Standard output for process written to file: " + output); LOG.info("Standard error for process written to file: " + error); ProcessBuilder processBuilder = new ProcessBuilder().redirectInput(input).redirectOutput(output) .redirectError(error).directory(tempDir).command(args); processBuilder.environment().putAll(env); LOG.info("Starting: " + args); delegate = processBuilder.start();/* www. j av a 2 s . c o m*/ ThreadUtil.sleep(100); // let it start outputTailer = new SimpleFileTailer(output); errorTailer = new SimpleFileTailer(error); }
From source file:org.apache.jxtadoop.util.Shell.java
/** Run a command */ private void runCommand() throws IOException { ProcessBuilder builder = new ProcessBuilder(getExecString()); boolean completed = false; if (environment != null) { builder.environment().putAll(this.environment); }//from w w w. ja v a 2s . co m if (dir != null) { builder.directory(this.dir); } process = builder.start(); 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 = true; if (exitCode != 0) { throw new ExitCodeException(exitCode, errMsg.toString()); } } catch (InterruptedException ie) { throw new IOException(ie.toString()); } finally { // close the input stream try { inReader.close(); } catch (IOException ioe) { LOG.warn("Error while closing the input stream", ioe); } if (!completed) { errThread.interrupt(); } try { errReader.close(); } catch (IOException ioe) { LOG.warn("Error while closing the error stream", ioe); } process.destroy(); lastTime = System.currentTimeMillis(); } }
From source file:org.eclipse.gyrex.jobs.internal.externalprocess.ExternalProcessJob.java
@Override protected IStatus run(final IProgressMonitor monitor) { final ProcessBuilder builder = new ProcessBuilder(); log.debug("Command: {}", command); builder.command(command);/*from w w w .java2 s .c o m*/ if (workingDirectory != null) { log.debug("Using working directory: {}", workingDirectory); builder.directory(workingDirectory); } if (clearEnvironment) { builder.environment().clear(); log.debug("Cleared environment!"); } else { // remove all Gyrex specific settings for security reasons final Iterator<Entry<String, String>> entries = builder.environment().entrySet().iterator(); while (entries.hasNext()) { final Map.Entry<java.lang.String, java.lang.String> e = entries.next(); if (StringUtils.startsWithIgnoreCase(e.getKey(), "gyrex")) { log.debug("Removing Gyrex specific environment variable: {}", e.getKey()); entries.remove(); } } } setEnvironmentVariable(builder, "WORKSPACE", Platform.getInstanceLocation().toOSString()); setEnvironmentVariable(builder, "JOB_ID", jobId); if (additionalEnvironment != null) { for (final Entry<String, String> e : additionalEnvironment.entrySet()) { log.debug("Additional environment variable: {} = {}", e.getKey(), e.getValue()); builder.environment().put(e.getKey(), e.getValue()); } } AsyncLoggingInputStreamReader inputStreamReader = null, errorStreamReader = null; try { final Process p = builder.start(); inputStreamReader = new AsyncLoggingInputStreamReader(jobId + " [OUT Reader]", p.getInputStream(), log, Level.INFO); errorStreamReader = new AsyncLoggingInputStreamReader(jobId + " [ERR Reader]", p.getErrorStream(), log, Level.ERROR); final int result = p.waitFor(); if (result != exitValue) return new Status(IStatus.ERROR, JobsActivator.SYMBOLIC_NAME, "Process finished with unexpected exit value: " + result); } catch (final InterruptedException e) { log.warn("Interrupted while waiting for the process to finish.", e); Thread.currentThread().interrupt(); return Status.CANCEL_STATUS; } catch (final Exception | AssertionError | LinkageError e) { log.error("Error starting process. {} ", e.getMessage(), e); return new Status(IStatus.ERROR, JobsActivator.SYMBOLIC_NAME, "Error starting process: " + e.getMessage(), e); } finally { if (inputStreamReader != null) { inputStreamReader.close(); } if (errorStreamReader != null) { errorStreamReader.close(); } } if (StringUtils.isNotBlank(inputStreamReader.getLastLine())) return new Status(IStatus.OK, JobsActivator.SYMBOLIC_NAME, inputStreamReader.getLastLine()); return Status.OK_STATUS; }
From source file:org.ballerinalang.test.context.BMainInstance.java
/** * Executing the sh or bat file to start the server. * * @param command command to run/* w w w .j a va 2 s . co m*/ * @param args command line arguments to pass when executing the sh or bat file * @param envProperties environmental properties to be appended to the environment * @param clientArgs arguments which program expects * @param leechers log leechers to check the log if any * @param commandDir where to execute the command * @throws BallerinaTestException if starting services failed */ public void runMain(String command, String[] args, Map<String, String> envProperties, String[] clientArgs, LogLeecher[] leechers, String commandDir) throws BallerinaTestException { String scriptName = Constant.BALLERINA_SERVER_SCRIPT_NAME; String[] cmdArray; try { if (Utils.getOSName().toLowerCase(Locale.ENGLISH).contains("windows")) { cmdArray = new String[] { "cmd.exe", "/c", balServer.getServerHome() + File.separator + "bin" + File.separator + scriptName + ".bat", command }; } else { cmdArray = new String[] { "bash", balServer.getServerHome() + File.separator + "bin/" + scriptName, command }; } String[] cmdArgs = Stream.concat(Arrays.stream(cmdArray), Arrays.stream(args)).toArray(String[]::new); ProcessBuilder processBuilder = new ProcessBuilder(cmdArgs).directory(new File(commandDir)); if (envProperties != null) { Map<String, String> env = processBuilder.environment(); for (Map.Entry<String, String> entry : envProperties.entrySet()) { env.put(entry.getKey(), entry.getValue()); } } Process process = processBuilder.start(); ServerLogReader serverInfoLogReader = new ServerLogReader("inputStream", process.getInputStream()); ServerLogReader serverErrorLogReader = new ServerLogReader("errorStream", process.getErrorStream()); if (leechers == null) { leechers = new LogLeecher[] {}; } for (LogLeecher leecher : leechers) { switch (leecher.getLeecherType()) { case INFO: serverInfoLogReader.addLeecher(leecher); break; case ERROR: serverErrorLogReader.addLeecher(leecher); break; } } serverInfoLogReader.start(); serverErrorLogReader.start(); if (clientArgs != null && clientArgs.length > 0) { writeClientArgsToProcess(clientArgs, process); } process.waitFor(); serverInfoLogReader.stop(); serverInfoLogReader.removeAllLeechers(); serverErrorLogReader.stop(); serverErrorLogReader.removeAllLeechers(); } catch (IOException e) { throw new BallerinaTestException("Error executing ballerina", e); } catch (InterruptedException e) { throw new BallerinaTestException("Error waiting for execution to finish", e); } }
From source file:org.lab41.graphlab.twill.GraphLabRunnable.java
private void runProcess(int instanceCount) throws ExecutionException, InterruptedException, IOException { FileSystem fileSystem = FileSystem.get(new Configuration()); File graphLabPath = arguments.getGraphLabPath(); Preconditions.checkNotNull(graphLabPath, "graphlab path is null"); Preconditions.checkArgument(graphLabPath.exists(), "graphlab path does not exist"); Path inputPath = arguments.getInputPath(); Preconditions.checkNotNull(inputPath, "input path is null"); Preconditions.checkNotNull(fileSystem.exists(inputPath), "input path does not exist"); String inputFormat = arguments.getInputFormat(); Preconditions.checkNotNull(inputFormat, "input format is null"); Path outputPath = arguments.getOutputPath(); Preconditions.checkNotNull(outputPath, "output path is null"); String zkStr = System.getenv("TWILL_ZK_CONNECT"); Preconditions.checkNotNull(zkStr);//from ww w .j ava 2s . c o m // Start building up the command line. List<String> args = new ArrayList<>(); args.add(graphLabPath.toString()); args.add("--graph"); args.add(inputPath.toString()); args.add("--format"); args.add(inputFormat); // We need to treat some algorithms specially. String commandName = graphLabPath.getName(); switch (commandName) { case "simple_coloring": args.add("--output"); args.add(outputPath.toString()); break; case "TSC": // do nothing. TSC outputs to stdout, so we'll have to capture it ourselves. break; default: args.add("--saveprefix"); args.add(outputPath.toString()); break; } ProcessBuilder processBuilder = new ProcessBuilder(args); Map<String, String> env = processBuilder.environment(); env.clear(); env.put("CLASSPATH", getHadoopClassPath()); env.put("ZK_SERVERS", zkStr); env.put("ZK_JOBNAME", "graphLab-workers"); env.put("ZK_NUMNODES", Integer.toString(instanceCount)); if (!commandName.equals("TSC")) { processBuilder.redirectErrorStream(true); } LOG.info("executing: " + args); Process process = processBuilder.start(); ExecutorService executor = Executors.newFixedThreadPool(2); try { Future<Void> stdoutFuture; if (commandName.equals("TSC")) { // The TSC outputs to stdout, so capture and redirect it to our file. stdoutFuture = executor .submit(captureInputStream(fileSystem, process.getInputStream(), outputPath)); } else { // Otherwise, write the output to the log file. stdoutFuture = executor.submit(logInputStream(process.getInputStream())); } // Also write the stderr to the log file. Future<Void> stderrFuture = executor.submit(logInputStream(process.getErrorStream())); // Ignore errors for now. int exitCode = process.waitFor(); LOG.info("process exited with " + exitCode); stdoutFuture.get(); stderrFuture.get(); } finally { executor.shutdown(); } }
From source file:com.clustercontrol.util.CommandExecutor.java
public Process execute() throws HinemosUnknown { // workaround for JVM(Windows) Bug // Runtime#exec is not thread safe on Windows. try {/*from w w w . j av a2s. com*/ synchronized (_runtimeExecLock) { ProcessBuilder pb = new ProcessBuilder(_command); // ???"_JAVA_OPTIONS"???? pb.environment().remove("_JAVA_OPTIONS"); for (Map.Entry<String, String> entry : envMap.entrySet()) { pb.environment().put(entry.getKey(), entry.getValue()); } process = pb.start(); } } catch (Exception e) { log.warn("command executor failure. (command = " + _commandLine + ") " + e.getMessage(), e); throw new HinemosUnknown(e.getMessage()); } return process; }
From source file:org.ballerinalang.test.context.BServerInstance.java
/** * Executing the sh or bat file to start the server. * * @param args - command line arguments to pass when executing the sh or bat file * @param envProperties - environmental properties to be appended to the environment * @param requiredPorts - ports required for the server instance * @throws BallerinaTestException if starting services failed *//*from w w w . j a va 2s .co m*/ private void startServer(String[] args, Map<String, String> envProperties, int[] requiredPorts) throws BallerinaTestException { if (requiredPorts == null) { requiredPorts = new int[] {}; } this.requiredPorts = ArrayUtils.addAll(requiredPorts, agentPort); //Check whether agent port is available. Utils.checkPortsAvailability(requiredPorts); log.info("Starting Ballerina server.."); String scriptName = Constant.BALLERINA_SERVER_SCRIPT_NAME; String[] cmdArray; File commandDir = new File(balServer.getServerHome()); try { if (Utils.getOSName().toLowerCase(Locale.ENGLISH).contains("windows")) { commandDir = new File(balServer.getServerHome() + File.separator + "bin"); cmdArray = new String[] { "cmd.exe", "/c", scriptName + ".bat", "run" }; } else { cmdArray = new String[] { "bash", "bin/" + scriptName, "run" }; } String[] cmdArgs = Stream.concat(Arrays.stream(cmdArray), Arrays.stream(args)).toArray(String[]::new); ProcessBuilder processBuilder = new ProcessBuilder(cmdArgs).directory(commandDir); if (envProperties != null) { Map<String, String> env = processBuilder.environment(); for (Map.Entry<String, String> entry : envProperties.entrySet()) { env.put(entry.getKey(), entry.getValue()); } } process = processBuilder.start(); serverInfoLogReader = new ServerLogReader("inputStream", process.getInputStream()); tmpInfoLeechers.forEach(leecher -> serverInfoLogReader.addLeecher(leecher)); serverInfoLogReader.start(); serverErrorLogReader = new ServerLogReader("errorStream", process.getErrorStream()); tmpErrorLeechers.forEach(leecher -> serverErrorLogReader.addLeecher(leecher)); serverErrorLogReader.start(); log.info("Waiting for port " + agentPort + " to open"); Utils.waitForPortsToOpen(new int[] { agentPort }, 1000 * 60 * 2, false, agentHost); log.info("Server Started Successfully."); } catch (IOException e) { throw new BallerinaTestException("Error starting services", e); } }
From source file:org.apache.flume.test.util.StagedInstall.java
public synchronized void startAgent(String name, Properties properties) throws Exception { if (process != null) { throw new Exception("A process is already running"); }/*w w w . jav a 2 s . c om*/ LOGGER.info("Starting process for agent: " + name + " using config: " + properties); File configFile = createConfigurationFile(name, properties); configFilePath = configFile.getCanonicalPath(); String configFileName = configFile.getName(); String logFileName = "flume-" + name + "-" + configFileName.substring(0, configFileName.indexOf('.')) + ".log"; LOGGER.info("Created configuration file: " + configFilePath); String[] cmdArgs = { launchScriptPath, "agent", "-n", name, "-f", configFilePath, "-c", confDirPath, "-D" + ENV_FLUME_LOG_DIR + "=" + logDirPath, "-D" + ENV_FLUME_ROOT_LOGGER + "=" + ENV_FLUME_ROOT_LOGGER_VALUE, "-D" + ENV_FLUME_LOG_FILE + "=" + logFileName }; StringBuilder sb = new StringBuilder(""); for (String cmdArg : cmdArgs) { sb.append(cmdArg).append(" "); } LOGGER.info("Using command: " + sb.toString()); ProcessBuilder pb = new ProcessBuilder(cmdArgs); Map<String, String> env = pb.environment(); LOGGER.debug("process environment: " + env); pb.directory(baseDir); pb.redirectErrorStream(true); process = pb.start(); consumer = new ProcessInputStreamConsumer(process.getInputStream()); consumer.start(); shutdownHook = new ProcessShutdownHook(); Runtime.getRuntime().addShutdownHook(shutdownHook); Thread.sleep(3000); // sleep for 3s to let system initialize }