Example usage for java.lang ProcessBuilder ProcessBuilder

List of usage examples for java.lang ProcessBuilder ProcessBuilder

Introduction

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

Prototype

ProcessBuilder

Source Link

Usage

From source file:azkaban.utils.FileIOUtils.java

/**
 * Run a unix command that will symlink files, and recurse into directories.
 *//*from  w  w  w .j a  va2s  .c om*/
public static void createDeepSymlink(File sourceDir, File destDir) throws IOException {
    if (!sourceDir.exists()) {
        throw new IOException("Source directory " + sourceDir.getPath() + " doesn't exist");
    } else if (!destDir.exists()) {
        throw new IOException("Destination directory " + destDir.getPath() + " doesn't exist");
    } else if (sourceDir.isFile() && destDir.isFile()) {
        throw new IOException("Source or Destination is not a directory.");
    }

    Set<String> paths = new HashSet<String>();
    createDirsFindFiles(sourceDir, sourceDir, destDir, paths);

    StringBuffer buffer = new StringBuffer();
    for (String path : paths) {
        File sourceLink = new File(sourceDir, path);
        path = "." + path;

        buffer.append("ln -s ").append(sourceLink.getAbsolutePath()).append("/*").append(" ").append(path)
                .append(";");
    }

    String command = buffer.toString();
    ProcessBuilder builder = new ProcessBuilder().command("sh", "-c", command);
    builder.directory(destDir);

    // XXX what about stopping threads ??
    Process process = builder.start();
    try {
        NullLogger errorLogger = new NullLogger(process.getErrorStream());
        NullLogger inputLogger = new NullLogger(process.getInputStream());
        errorLogger.start();
        inputLogger.start();

        try {
            if (process.waitFor() < 0) {
                // Assume that the error will be in standard out. Otherwise it'll be
                // in standard in.
                String errorMessage = errorLogger.getLastMessages();
                if (errorMessage.isEmpty()) {
                    errorMessage = inputLogger.getLastMessages();
                }

                throw new IOException(errorMessage);
            }

            // System.out.println(errorLogger.getLastMessages());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    } finally {
        IOUtils.closeQuietly(process.getInputStream());
        IOUtils.closeQuietly(process.getOutputStream());
        IOUtils.closeQuietly(process.getErrorStream());
    }
}

From source file:com.amazonaws.eclipse.dynamodb.testtool.TestToolProcess.java

/**
 * Start the DynamoDBLocal process.//from  w w  w. j a  v  a2s.c o m
 *
 * @param  onExitAction optional action to be executed when the process
 *                      exits
 * @throws IOException  if starting the process fails
 */
public synchronized void start(final Runnable onExitAction) throws IOException {

    if (process != null) {
        throw new IllegalStateException("Already started!");
    }

    ProcessBuilder builder = new ProcessBuilder();

    builder.directory(installDirectory);
    builder.command(jre.getInstallLocation().getAbsolutePath().concat("/bin/java"),
            "-Djava.library.path=".concat(findLibraryDirectory().getAbsolutePath()), "-jar",
            "DynamoDBLocal.jar", "--port", Integer.toString(port));

    // Drop STDERR into STDOUT so we can handle them together.
    builder.redirectErrorStream(true);

    // Register a shutdown hook to kill DynamoDBLocal if Eclipse exits.
    Runtime.getRuntime().addShutdownHook(shutdownHook);

    // Start the DynamoDBLocal process.
    process = builder.start();

    // Start a background thread to read any output from DynamoDBLocal
    // and dump it to an IConsole.
    new ConsoleOutputLogger(process.getInputStream(), onExitAction).start();
}

From source file:org.elasticsearch.qa.die_with_dignity.DieWithDignityIT.java

public void testDieWithDignity() throws Exception {
    // deleting the PID file prevents stopping the cluster from failing since it occurs if and only if the PID file exists
    final Path pidFile = PathUtils.get(System.getProperty("pidfile"));
    final List<String> pidFileLines = Files.readAllLines(pidFile);
    assertThat(pidFileLines, hasSize(1));
    final int pid = Integer.parseInt(pidFileLines.get(0));
    Files.delete(pidFile);//  w w  w  .j  av  a  2 s . c om
    IOException e = expectThrows(IOException.class,
            () -> client().performRequest(new Request("GET", "/_die_with_dignity")));
    Matcher<IOException> failureMatcher = instanceOf(ConnectionClosedException.class);
    if (Constants.WINDOWS) {
        /*
         * If the other side closes the connection while we're waiting to fill our buffer
         * we can get IOException with the message below. It seems to only come up on
         * Windows and it *feels* like it could be a ConnectionClosedException but
         * upstream does not consider this a bug:
         * https://issues.apache.org/jira/browse/HTTPASYNC-134
         *
         * So we catch it here and consider it "ok".
        */
        failureMatcher = either(failureMatcher).or(
                hasToString(containsString("An existing connection was forcibly closed by the remote host")));
    }
    assertThat(e, failureMatcher);

    // the Elasticsearch process should die and disappear from the output of jps
    assertBusy(() -> {
        final String jpsPath = PathUtils.get(System.getProperty("runtime.java.home"), "bin/jps").toString();
        final Process process = new ProcessBuilder().command(jpsPath).start();
        assertThat(process.waitFor(), equalTo(0));
        try (InputStream is = process.getInputStream();
                BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8"))) {
            String line;
            while ((line = in.readLine()) != null) {
                final int currentPid = Integer.parseInt(line.split("\\s+")[0]);
                assertThat(line, pid, not(equalTo(currentPid)));
            }
        }
    });

    // parse the logs and ensure that Elasticsearch died with the expected cause
    final List<String> lines = Files.readAllLines(PathUtils.get(System.getProperty("log")));

    final Iterator<String> it = lines.iterator();

    boolean fatalErrorOnTheNetworkLayer = false;
    boolean fatalErrorInThreadExiting = false;

    while (it.hasNext() && (fatalErrorOnTheNetworkLayer == false || fatalErrorInThreadExiting == false)) {
        final String line = it.next();
        if (line.contains("fatal error on the network layer")) {
            fatalErrorOnTheNetworkLayer = true;
        } else if (line.matches(".*\\[ERROR\\]\\[o.e.b.ElasticsearchUncaughtExceptionHandler\\] \\[node-0\\]"
                + " fatal error in thread \\[Thread-\\d+\\], exiting$")) {
            fatalErrorInThreadExiting = true;
            assertTrue(it.hasNext());
            assertThat(it.next(), equalTo("java.lang.OutOfMemoryError: die with dignity"));
        }
    }

    assertTrue(fatalErrorOnTheNetworkLayer);
    assertTrue(fatalErrorInThreadExiting);
}

From source file:org.fcrepo.it.SparqlRecipesIT.java

@BeforeClass
public static void startFuseki() throws InterruptedException, IOException {

    //Determine the snapshot used for testing and its REST api,
    //make it default for snapshot 4 for the current version
    final String fcrepoSnapshot = System.getProperty("fcrepo.version");
    if (fcrepoSnapshot != null && fcrepoSnapshot.indexOf("-") > 0
            && fcrepoSnapshot.indexOf("4.0.0-beta") >= 0) {
        final String[] verTokens = fcrepoSnapshot.split("-");
        if (verTokens.length >= 3) {
            try {
                FCREPO_SNAPSHOT_NUMBER = Short.parseShort(verTokens[2]);
            } catch (final NumberFormatException ne) {
                FCREPO_SNAPSHOT_NUMBER = 4;
            }//from   ww  w. jav  a2  s. c o m
        }
    }
    if (FCREPO_SNAPSHOT_NUMBER < 4) {
        DATASTREAM_URL_SUFIX = "";
        DATASTREAM_CONTENT_URL_SUFIX = "/fcr:content";
        DATASTREAM_MIXIN_TYPE = "fedora:datastream";
        DATASTREAM_RELATION = "fcrepo:hasContent";
    }

    final File commandFile = new File("target/jena-fuseki-1.0.1/fuseki-server");
    final ProcessBuilder b = new ProcessBuilder().inheritIO().directory(commandFile.getParentFile()).command(
            "./fuseki-server", "--update", "--mem", "--port=" + FUSEKI_PORT, "--mgtPort=" + MGT_PORT, "/test");
    fuseki = b.start();

    // It might take a while to startup and be ready to receive messages...
    Thread.sleep(10000);

    setUpTestObjects();
}

From source file:net.dv8tion.jda.player.source.RemoteSource.java

@Override
public synchronized AudioInfo getInfo() {
    if (audioInfo != null)
        return audioInfo;

    List<String> infoArgs = new LinkedList<>();
    if (ytdlLaunchArgsF != null) {
        infoArgs.addAll(ytdlLaunchArgsF);
        if (!infoArgs.contains("-q"))
            infoArgs.add("-q");
    } else/*from   w w  w  .  j  a  va2 s. c o m*/
        infoArgs.addAll(YOUTUBE_DL_LAUNCH_ARGS);

    infoArgs.add("--ignore-errors"); //Ignore errors, obviously
    infoArgs.add("-j"); //Dumps the json about the file into STDout
    infoArgs.add("--skip-download"); //Doesn't actually download the file.
    infoArgs.add("--"); //Url separator. Deals with YT ids that start with --
    infoArgs.add(url); //specifies the URL to download.

    audioInfo = new AudioInfo();
    try {
        Process infoProcess = new ProcessBuilder().command(infoArgs).start();
        byte[] infoData = IOUtils.readFully(infoProcess.getErrorStream(), -1, false); //YT-DL outputs to STDerr
        if (infoData == null || infoData.length == 0)
            throw new NullPointerException("The Youtube-DL process resulted in a null or zero-length INFO!");

        String infoString = new String(infoData);
        if (infoString.startsWith("ERROR")) {
            audioInfo.error = infoString;
        } else {
            JSONObject info = new JSONObject(infoString);

            audioInfo.jsonInfo = info;
            audioInfo.title = !info.optString("title", "").isEmpty() ? info.getString("title")
                    : !info.optString("fulltitle", "").isEmpty() ? info.getString("fulltitle") : null;
            audioInfo.origin = !info.optString("webpage_url", "").isEmpty() ? info.getString("webpage_url")
                    : url;
            audioInfo.id = !info.optString("id", "").isEmpty() ? info.getString("id") : null;
            audioInfo.encoding = !info.optString("acodec", "").isEmpty() ? info.getString("acodec")
                    : !info.optString("ext", "").isEmpty() ? info.getString("ext") : null;
            audioInfo.description = !info.optString("description", "").isEmpty() ? info.getString("description")
                    : null;
            audioInfo.extractor = !info.optString("extractor", "").isEmpty() ? info.getString("extractor")
                    : !info.optString("extractor_key").isEmpty() ? info.getString("extractor_key") : null;
            audioInfo.thumbnail = !info.optString("thumbnail", "").isEmpty() ? info.getString("thumbnail")
                    : null;
            audioInfo.isLive = info.has("is_live") && !info.isNull("is_live") && info.getBoolean("is_live");
            audioInfo.duration = info.optInt("duration", -1) != -1
                    ? AudioTimestamp.fromSeconds(info.getInt("duration"))
                    : null;

            //Use FFprobe to find the duration because YT-DL didn't give it to us.
            if (audioInfo.duration == null) {
                List<String> ffprobeInfoArgs = new LinkedList<>();
                ffprobeInfoArgs.addAll(LocalSource.FFPROBE_INFO_ARGS);
                ffprobeInfoArgs.add("-i");
                ffprobeInfoArgs.add(info.optString("url", url));

                infoProcess = new ProcessBuilder().command(ffprobeInfoArgs).start();
                infoData = IOUtils.readFully(infoProcess.getInputStream(), -1, false);
                if (infoData != null && infoData.length > 0) {
                    info = new JSONObject(new String(infoData)).getJSONObject("format");

                    if (info.optDouble("duration", -1.0) != -1.0) {
                        int duration = Math.round((float) info.getDouble("duration"));
                        audioInfo.duration = AudioTimestamp.fromSeconds(duration);
                    }
                }
            }
        }
    } catch (IOException e) {
        audioInfo.error = e.getMessage();
        e.printStackTrace();
    } catch (JSONException e) {
        audioInfo.error = e.getMessage();
        e.printStackTrace();
    }
    return audioInfo;
}

From source file:io.github.retz.executor.FileManager.java

private static void fetchHDFSFile(String file, String dest) throws IOException {
    LOG.debug("Downloading {} to {} as HDFS file", file, dest);
    // TODO: make 'hadoop' command arbitrarily specifiable, but given that mesos-agent (slave) can fetch hdfs:// files, it should be also available, too
    String[] hadoopCmd = { "hadoop", "fs", "-copyToLocal", file, dest };
    LOG.debug("Command: {}", String.join(" ", hadoopCmd));
    ProcessBuilder pb = new ProcessBuilder();
    pb.command(hadoopCmd).inheritIO();/*w w w . j  ava  2 s  .  c o m*/

    Process p = pb.start();
    while (true) {
        try {
            int result = p.waitFor();
            if (result != 0) {
                LOG.error("Downloading {} failed: {}", file, result);
            } else {
                LOG.info("Download finished: {}", file);
            }
            return;
        } catch (InterruptedException e) {
            LOG.error("Download process interrupted: {}", e.getMessage()); // TODO: debug?
        }
    }
}

From source file:com.amazonaws.services.kinesis.multilang.StreamingRecordProcessorTest.java

@Before
public void prepare() throws IOException, InterruptedException, ExecutionException {
    // Fake command
    String command = "derp";
    systemExitCount = 0;//  w  w w.  ja v  a 2s  . c om

    // Mocks
    ExecutorService executor = Executors.newFixedThreadPool(3);
    final Process process = Mockito.mock(Process.class);

    messageWriter = Mockito.mock(MessageWriter.class);
    messageReader = Mockito.mock(MessageReader.class);
    errorReader = Mockito.mock(DrainChildSTDERRTask.class);

    recordProcessor = new MultiLangRecordProcessor(new ProcessBuilder(), executor, new ObjectMapper(),
            messageWriter, messageReader, errorReader) {

        // Just don't do anything when we exit.
        void exit() {
            systemExitCount += 1;
        }

        // Inject our mock process
        Process startProcess() {
            return process;
        }
    };

    // Our process will return mock streams
    InputStream inputStream = Mockito.mock(InputStream.class);
    InputStream errorStream = Mockito.mock(InputStream.class);
    OutputStream outputStream = Mockito.mock(OutputStream.class);
    Mockito.doReturn(inputStream).when(process).getInputStream();
    Mockito.doReturn(errorStream).when(process).getErrorStream();
    Mockito.doReturn(outputStream).when(process).getOutputStream();

    Mockito.doReturn(Mockito.mock(Future.class)).when(messageReader).drainSTDOUT();
    Future<Boolean> trueFuture = Mockito.mock(Future.class);
    Mockito.doReturn(true).when(trueFuture).get();

    Mockito.doReturn(trueFuture).when(messageWriter).writeInitializeMessage(Mockito.anyString());
    Mockito.doReturn(trueFuture).when(messageWriter).writeCheckpointMessageWithError(Mockito.anyString(),
            Mockito.any(Throwable.class));
    Mockito.doReturn(trueFuture).when(messageWriter).writeProcessRecordsMessage(Mockito.anyList());
    Mockito.doReturn(trueFuture).when(messageWriter).writeShutdownMessage(Mockito.any(ShutdownReason.class));
}

From source file:com.baifendian.swordfish.execserver.job.ProcessJob.java

/**
 * ?, ? shell //from w w  w . j a  va2  s  .c  o  m
 *
 * @param command ?
 * @return ??, 0 ?, 
 */
public int runCommand(String command) {
    // , 
    long remainTime = calcNodeTimeout();
    int exitCode;

    try {
        // ?
        processBuilder = new ProcessBuilder();

        // ? job ?
        if (StringUtils.isEmpty(command)) {
            exitCode = 0;
            return exitCode;
        }

        // ?
        String commandFile = String.format("%s/%s.command", workDir, jobAppId);

        logger.info("proxy user:{}, work dir:{}", proxyUser, workDir);

        // ?, ??
        if (!Files.exists(Paths.get(commandFile))) {
            logger.info("generate command file:{}", commandFile);

            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("#!/bin/sh\n");
            stringBuilder.append("BASEDIR=$(cd `dirname $0`; pwd)\n");
            stringBuilder.append("cd $BASEDIR\n");

            if (envFile != null) {
                stringBuilder.append("source " + envFile + "\n");
            }

            stringBuilder.append("\n\n");
            stringBuilder.append(command);

            // ?
            FileUtils.writeStringToFile(new File(commandFile), stringBuilder.toString(),
                    Charset.forName("UTF-8"));
        }

        // ?

        processBuilder.command("sudo", "-u", proxyUser, "sh", commandFile);

        // 
        processBuilder.directory(new File(workDir));

        //  error ? merge ?
        processBuilder.redirectErrorStream(true);
        process = processBuilder.start();

        // ??
        printCommand(processBuilder);

        // ??
        readProcessOutput();

        int pid = getProcessId(process);

        logger.info("Process start, process id is: {}", pid);

        // 
        if (isLongJob) {
            // ?, , ??
            // ?,  10 , ?
            while (!isCompleted.getAsBoolean() && process.isAlive()) {
                Thread.sleep(3000);
            }

            logger.info("streaming job has exit, work dir:{}, pid:{}", workDir, pid);

            // ?,  storm, ????
            exitCode = (isCompleted.getAsBoolean()) ? 0 : -1;
        } else {// ?
            boolean status = process.waitFor(remainTime, TimeUnit.SECONDS);

            if (status) {
                exitCode = process.exitValue();
                logger.info("job has exit, work dir:{}, pid:{}", workDir, pid);
            } else {
                cancel();
                exitCode = -1;
                logger.info("job has timeout, work dir:{}, pid:{}", workDir, pid);
            }
        }
    } catch (InterruptedException e) {
        logger.error("interrupt exception, maybe task has been cancel or killed.", e);
        exitCode = -1;
        throw new ExecException("Process has been interrupted. Exit code is " + exitCode);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        exitCode = -1;
        throw new ExecException("Process error. Exit code is " + exitCode);
    }

    return exitCode;
}

From source file:com.lithium.flow.vault.AgentVault.java

private void startAgent(@Nonnull String password) {
    try {//from ww w  . j a  v  a 2s .c  o  m
        int port = findFreePort();
        String agentPassword = Vaults.securePassword();

        Map<String, String> map = new HashMap<>();
        Store agentStore = new MemoryStore(map);
        Vault agentVault = new SecureVault(Configs.empty(), agentStore);
        agentVault.setup(agentPassword);
        agentVault.putValue("password", password);

        ProcessBuilder builder = new ProcessBuilder();
        builder.command(System.getProperty("java.home") + "/bin/java", "-Dagent.port=" + port,
                AgentServer.class.getName());
        builder.environment().put("CLASSPATH", System.getProperty("java.class.path"));
        Process process = builder.start();

        OutputStream out = process.getOutputStream();
        mapper.writeValue(out, map);
        out.close();

        store.putValue("vault.agent.port", String.valueOf(port));
        store.putValue("vault.agent.password", agentPassword);
    } catch (IOException e) {
        throw new VaultException("failed to start agent", e);
    }
}

From source file:de.zib.gndms.logic.model.gorfx.c3grid.ExternalProviderStageInORQCalculator.java

private ProcessBuilderAction createEstAction(final File estCommandFileParam,
        final TransientContract contParam) {
    final @NotNull ProcessBuilder pb = new ProcessBuilder();
    try {//  w  w w.  j ava 2  s. c  om
        pb.command(estCommandFileParam.getCanonicalPath());
        pb.directory(new File(getSysInfo().getSystemTempDirName()));
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }

    ProcessBuilderAction action;
    // todo add permissions here when delegation is implemented
    action = parmAux.createPBAction(getORQArguments(), contParam, null);
    action.setProcessBuilder(pb);
    action.setOutputReceiver(new StringBuilder(INITIAL_STRING_BUILDER_CAPACITY));
    action.setErrorReceiver(new StringBuilder(INITIAL_STRING_BUILDER_CAPACITY));
    return action;
}