Example usage for java.lang ProcessBuilder redirectOutput

List of usage examples for java.lang ProcessBuilder redirectOutput

Introduction

In this page you can find the example usage for java.lang ProcessBuilder redirectOutput.

Prototype

public ProcessBuilder redirectOutput(File file) 

Source Link

Document

Sets this process builder's standard output destination to a file.

Usage

From source file:org.craftercms.studio.impl.v1.deployment.EnvironmentStoreGitBranchDeployer.java

private void createPatch(Repository repository, String site, String path) {
    StringBuffer output = new StringBuffer();

    String tempPath = System.getProperty("java.io.tmpdir");
    if (tempPath == null) {
        tempPath = "temp";
    }//from   w ww.java  2 s  . co m
    Path patchPath = Paths.get(tempPath, "patch" + site + ".bin");

    String gitPath = getGitPath(path);
    Process p = null;
    File file = patchPath.toAbsolutePath().normalize().toFile();
    try {
        ProcessBuilder pb = new ProcessBuilder();
        pb.command("git", "diff", "--binary", environment, "master", "--", gitPath);

        pb.redirectOutput(file);
        pb.directory(repository.getDirectory().getParentFile());
        p = pb.start();
        p.waitFor();
    } catch (Exception e) {
        logger.error("Error while creating patch for site: " + site + " path: " + path, e);
    }
}

From source file:org.apache.hadoop.hive.llap.cli.service.LlapServiceDriver.java

private int runPackagePy(Path tmpDir, Path scriptParent, String version, String outputDir)
        throws IOException, InterruptedException {
    Path scriptPath = new Path(new Path(scriptParent, "yarn"), "package.py");
    List<String> scriptArgs = new ArrayList<>(cl.getArgs().length + 7);
    scriptArgs.addAll(Arrays.asList("python", scriptPath.toString(), "--input", tmpDir.toString(), "--output",
            outputDir, "--javaChild"));
    scriptArgs.addAll(Arrays.asList(cl.getArgs()));

    LOG.debug("Calling package.py via: " + scriptArgs);
    ProcessBuilder builder = new ProcessBuilder(scriptArgs);
    builder.redirectError(ProcessBuilder.Redirect.INHERIT);
    builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
    builder.environment().put("HIVE_VERSION", version);
    return builder.start().waitFor();
}

From source file:org.opencb.cellbase.app.cli.CommandExecutor.java

private ProcessBuilder getProcessBuilder(File workingDirectory, String binPath, List<String> args,
        String logFilePath) {//from   www.ja v  a  2s  . c  o  m
    List<String> commandArgs = new ArrayList<>();
    commandArgs.add(binPath);
    commandArgs.addAll(args);
    ProcessBuilder builder = new ProcessBuilder(commandArgs);

    // working directoy and error and output log outputs
    if (workingDirectory != null) {
        builder.directory(workingDirectory);
    }
    builder.redirectErrorStream(true);
    if (logFilePath != null) {
        builder.redirectOutput(ProcessBuilder.Redirect.appendTo(new File(logFilePath)));
    }

    return builder;
}

From source file:org.opencb.bionetdb.app.cli.CommandExecutor.java

@Deprecated
private ProcessBuilder getProcessBuilder(File workingDirectory, String binPath, List<String> args,
        String logFilePath) {/*from  ww  w.jav a  2 s  .  c o m*/
    List<String> commandArgs = new ArrayList<>();
    commandArgs.add(binPath);
    commandArgs.addAll(args);
    ProcessBuilder builder = new ProcessBuilder(commandArgs);

    // working directoy and error and output log outputs
    if (workingDirectory != null) {
        builder.directory(workingDirectory);
    }
    builder.redirectErrorStream(true);
    if (logFilePath != null) {
        builder.redirectOutput(ProcessBuilder.Redirect.appendTo(new File(logFilePath)));
    }

    return builder;
}

From source file:com.ecofactor.qa.automation.platform.ops.impl.AndroidOperations.java

/**
 * Start Appium server.//from w  w  w .  ja va  2s .c  om
 * @see com.ecofactor.qa.automation.mobile.ops.impl.AbstractMobileOperations#startAppiumServer()
 */
@Override
public void startAppiumServer() {

    setLogString(LogSection.START, "Start appium server", true);
    final String deviceId = getDeviceIdParam();
    if (!deviceProps.get(deviceId + DEVICE_STATE).equals("Online")) {
        setLogString("Appium will not be started since the device state is = "
                + deviceProps.get(deviceId + DEVICE_STATE) + ".", true);
        return;
    }
    final String appiumHome = System.getenv("APPIUM_HOME");
    setLogString("APPIUM_HOME : " + appiumHome, true);
    if (appiumHome == null || appiumHome.isEmpty()) {
        setErrorMsg("\033[41;1mPlease set APPIUM_HOME environment variable and try again.");
        setLogString(errorMsg, true);
        hasErrors = true;
        return;
    }
    generateNodeConfig();
    final List<String> commands = arrayToList("node", ".", "-U", deviceId, "-p", DEFAULT_PORT, "--full-reset");
    String listToString = StringUtil.listToString(commands, " ");
    final int indexOfK = listToString.lastIndexOf("/k");
    listToString = listToString.substring(indexOfK + 2, listToString.length());
    setLogString("Command to Start Appium Server:" + listToString, true);

    final ProcessBuilder process = new ProcessBuilder(commands);
    process.directory(new File(appiumHome));
    process.redirectOutput(new File("outPut.txt"));
    process.redirectError(new File("error.txt"));
    startProcessBuilder(process);
    mediumWait();
    setLogString(LogSection.END, "Started appium server", true);
}

From source file:com.diversityarrays.kdxplore.trialdesign.JobRunningTask.java

@Override
public Either<String, AlgorithmRunResult> generateResult(Closure<Void> arg0) throws Exception {

    AlgorithmRunResult result = new AlgorithmRunResult(algorithmName, algorithmFolder);
    ProcessBuilder pb = new ProcessBuilder(command);

    File tempAlgorithmOutputFile = new File(algorithmFolder, "stdout.txt");
    File tempAlgorithmErrorFile = new File(algorithmFolder, "stderr.txt");

    //pb.redirectErrorStream(true);
    tempAlgorithmErrorFile.createNewFile();
    tempAlgorithmOutputFile.createNewFile();

    pb.redirectOutput(tempAlgorithmOutputFile);
    pb.redirectError(tempAlgorithmErrorFile);

    Process process = pb.start();

    while (!process.waitFor(1000, TimeUnit.MILLISECONDS)) {
        if (backgroundRunner.isCancelRequested()) {
            process.destroy();/*w  w  w  .java 2 s.  c  om*/
            throw new CancellationException();
        }
    }

    int exitCode = process.exitValue();
    if (exitCode != 0) {
        String errtxt = Algorithms.readContent("Error Output: (code=" + exitCode + ")",
                new FileInputStream(tempAlgorithmErrorFile));
        return Either.left(errtxt);
    }

    if (!kdxploreOutputFile.exists()) {
        return Either.left("Missing output file: " + kdxploreOutputFile.getPath());
    }

    result.addTrialEntries(kdxploreOutputFile, userTrialEntries);

    return Either.right(result);
}

From source file:de.huberlin.wbi.hiway.common.Worker.java

private int exec() {
    File script = new File("./" + containerId);
    script.setExecutable(true);//  ww  w .  j a v  a 2  s.  c o  m
    ProcessBuilder processBuilder = new ProcessBuilder(script.getPath());
    processBuilder.directory(new File("."));
    Process process;
    int exitValue = -1;
    try {
        File stdOutFile = new File(Invocation.STDOUT_FILENAME);
        File stdErrFile = new File(Invocation.STDERR_FILENAME);
        processBuilder.redirectOutput(stdOutFile);
        processBuilder.redirectError(stdErrFile);
        process = processBuilder.start();
        exitValue = process.waitFor();
    } catch (IOException | InterruptedException e) {
        e.printStackTrace();
        System.exit(-1);
    }

    return exitValue;
}

From source file:com.nesscomputing.db.postgres.embedded.EmbeddedPostgreSQL.java

private void startPostmaster() throws IOException {
    final StopWatch watch = new StopWatch();
    watch.start();//from  w w  w .  j av  a 2  s .c  o m
    Preconditions.checkState(started.getAndSet(true) == false, "Postmaster already started");

    final List<String> args = Lists.newArrayList(pgBin("postgres"), "-D", dataDirectory.getPath(), "-p",
            Integer.toString(port), "-i", "-F");

    for (final Entry<String, String> config : postgresConfig.entrySet()) {
        args.add("-c");
        args.add(config.getKey() + "=" + config.getValue());
    }

    final ProcessBuilder builder = new ProcessBuilder(args);
    builder.redirectErrorStream(true);
    builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
    postmaster = builder.start();
    LOG.info("{} postmaster started as {} on port {}.  Waiting up to {}ms for server startup to finish.",
            instanceId, postmaster.toString(), port, PG_STARTUP_WAIT_MS);

    Runtime.getRuntime().addShutdownHook(newCloserThread());

    waitForServerStartup(watch);
}

From source file:com.ecofactor.qa.automation.platform.ops.impl.IOSOperations.java

/**
 * Start Appium server./*from   w  w  w .  j a  v a 2 s.c  o m*/
 * @see com.ecofactor.qa.automation.mobile.ops.impl.AbstractMobileOperations#startAppiumServer()
 */
@Override
public void startAppiumServer() {

    final String deviceId = getDeviceIdParam();
    startProxyServer(deviceId);
    setLogString(LogSection.START, "Start appium server", true);
    try {
        final ProcessBuilder process = new ProcessBuilder(
                arrayToList("node", ".", "-U", deviceId, "-p", DEFAULT_PORT));
        process.directory(new File("/Applications/Appium.app/Contents/Resources/node_modules/appium/"));
        process.redirectOutput(new File("outPut.txt"));
        process.redirectError(new File("error.txt"));
        startProcessBuilder(process);
    } catch (Exception e) {
        LOGGER.error("ERROR in starting Appium Server. Cause: ", e);
    }
    setLogString(LogSection.END, "Appium server started", true);
}

From source file:org.dawnsci.commandserver.core.process.ProgressableProcess.java

protected void pkill(int pid, String dir) throws Exception {

    // Use pkill, seems to kill all of the tree more reliably
    ProcessBuilder pb = new ProcessBuilder();

    // Can adjust env if needed:
    // Map<String, String> env = pb.environment();
    pb.directory(new File(dir));

    File log = new File(dir, "xia2_kill.txt");
    pb.redirectErrorStream(true);//www.ja  v a  2 s  .c o  m
    pb.redirectOutput(Redirect.appendTo(log));

    pb.command("bash", "-c", "pkill -9 -s " + pid);

    Process p = pb.start();
    p.waitFor();
}