List of usage examples for java.lang Process exitValue
public abstract int exitValue();
From source file:org.apache.karaf.kittests.Helper.java
public static void waitForProcessEnd(Process process, long timeout) throws Exception { for (int i = 0; i < timeout / 100; i++) { try {//from ww w . j a v a 2 s.c om process.exitValue(); return; } catch (IllegalThreadStateException e) { } Thread.sleep(100); } throw new Exception("Process is still running"); }
From source file:org.mskcc.cbio.importer.util.Shell.java
/** * Executes the given command via java ProcessBuilder. * * @param command List<String>// w w w. j ava 2s . c o m * @param workingDirectory String * @return boolean */ public static boolean exec(List<String> command, String workingDirectory) { if (LOG.isInfoEnabled()) { LOG.info("exec():, command: " + command); LOG.info("exec():, workingDirectory: " + workingDirectory); } try { ProcessBuilder processBuilder = new ProcessBuilder(command); processBuilder.directory(new File(workingDirectory)); Process process = processBuilder.start(); StreamSink stdoutSink = new StreamSink(process.getInputStream()); StreamSink stderrSink = new StreamSink(process.getErrorStream()); stdoutSink.start(); stderrSink.start(); process.waitFor(); return process.exitValue() == 0; } catch (Exception e) { if (LOG.isInfoEnabled()) { LOG.info(e.toString() + " thrown during shell command: " + command); } return false; } }
From source file:com.basp.trabajo_al_minuto.model.business.BusinessUtils.java
public static int executeCommandLine(String file, String dir) throws BusinessException { try {/* ww w .jav a2 s .c o m*/ Process process = Runtime.getRuntime().exec(file, null, new File(dir)); process.waitFor(); return process.exitValue(); } catch (IOException | InterruptedException ex) { Logger.getLogger(BusinessUtils.class.getName()).log(Level.SEVERE, "BusinessUtils.executeCommandLine Exception", ex); throw new BusinessException(ex); } }
From source file:com.netscape.cmsutil.util.Utils.java
public static boolean exec(String cmd) { try {//from w w w.j ava2 s .c om String cmds[] = null; if (isNT()) { // NT cmds = new String[3]; cmds[0] = "cmd"; cmds[1] = "/c"; cmds[2] = cmd; } else { // UNIX cmds = new String[3]; cmds[0] = "/bin/sh"; cmds[1] = "-c"; cmds[2] = cmd; } Process process = Runtime.getRuntime().exec(cmds); process.waitFor(); if (process.exitValue() == 0) { /** * pOut = new BufferedReader( * new InputStreamReader(process.getInputStream())); * while ((l = pOut.readLine()) != null) { * System.out.println(l); * } **/ return true; } else { /** * pOut = new BufferedReader( * new InputStreamReader(process.getErrorStream())); * l = null; * while ((l = pOut.readLine()) != null) { * System.out.println(l); * } **/ return false; } } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } return false; }
From source file:com.ikanow.aleph2.data_model.utils.ProcessUtils.java
/** * Starts the given process by calling process_builder.start(); * Records the started processes pid and start date. * //from w ww . ja v a 2s . c om * @param process_builder * @throws IOException * @return returns any error in _1(), the pid in _2() */ public static Tuple2<String, String> launchProcess(final ProcessBuilder process_builder, final String application_name, final DataBucketBean bucket, final String aleph_root_path, final Optional<Tuple2<Long, Integer>> timeout_kill) { try { if (timeout_kill.isPresent()) { final Stream<String> timeout_stream = Stream.of("timeout", "-s", timeout_kill.get()._2.toString(), timeout_kill.get()._1.toString()); process_builder.command(Stream.concat(timeout_stream, process_builder.command().stream()) .collect(Collectors.toList())); } //starts the process, get pid back logger.debug("Starting process: " + process_builder.command().toString()); final Process px = process_builder.start(); String err = null; String pid = null; if (!px.isAlive()) { err = "Unknown error: " + px.exitValue() + ": " + process_builder.command().stream().collect(Collectors.joining(" ")); // (since not capturing output) } else { pid = getPid(px); //get the date on the pid file from /proc/<pid> final long date = getDateOfPid(pid); //record pid=date to aleph_root_path/pid_manager/bucket._id/application_name storePid(application_name, bucket, aleph_root_path, pid, date); } return Tuples._2T(err, pid); } catch (Throwable t) { return Tuples._2T(ErrorUtils.getLongForm("{0}", t), null); } }
From source file:com.impetus.ankush.agent.utils.CommandExecutor.java
/** * Method to execute command arrays.//ww w . java 2s .c o m * * @param command * @return * @throws IOException * @throws InterruptedException */ public static Result executeCommand(String... command) throws IOException, InterruptedException { Result rs = new Result(); Runtime rt = Runtime.getRuntime(); Process proc = rt.exec(command); StreamWrapper error = new StreamWrapper(proc.getErrorStream()); StreamWrapper output = new StreamWrapper(proc.getInputStream()); error.start(); output.start(); error.join(SLEEP_TIME); output.join(SLEEP_TIME); proc.waitFor(); rs.setExitVal(proc.exitValue()); rs.setOutput(output.getMessage()); rs.setError(error.getMessage()); proc.destroy(); return rs; }
From source file:com.impetus.ankush.agent.utils.CommandExecutor.java
/** * Method executeCommand./*from w w w. ja va 2s .c om*/ * * @param command * String * @return Result * @throws IOException * Signals that an I/O exception has occurred. * @throws InterruptedException * the interrupted exception */ public static Result executeCommand(String command) throws IOException, InterruptedException { Result rs = new Result(); Runtime rt = Runtime.getRuntime(); Process proc = rt.exec(command); StreamWrapper error = new StreamWrapper(proc.getErrorStream()); StreamWrapper output = new StreamWrapper(proc.getInputStream()); error.start(); output.start(); error.join(SLEEP_TIME); output.join(SLEEP_TIME); proc.waitFor(); rs.setExitVal(proc.exitValue()); rs.setOutput(output.getMessage()); rs.setError(error.getMessage()); proc.destroy(); return rs; }
From source file:Main.java
public static String highlight(final List<String> lines, final String meta, final String prog, final String encoding) throws IOException { final File tmpIn = new File(System.getProperty("java.io.tmpdir"), String.format("txtmark_code_%d_%d.in", ID, IN_COUNT.incrementAndGet())); final File tmpOut = new File(System.getProperty("java.io.tmpdir"), String.format("txtmark_code_%d_%d.out", ID, OUT_COUNT.incrementAndGet())); try {/*from w w w. j av a 2 s . c o m*/ final Writer w = new OutputStreamWriter(new FileOutputStream(tmpIn), encoding); try { for (final String s : lines) { w.write(s); w.write('\n'); } } finally { w.close(); } final List<String> command = new ArrayList<String>(); command.add(prog); command.add(meta); command.add(tmpIn.getAbsolutePath()); command.add(tmpOut.getAbsolutePath()); final ProcessBuilder pb = new ProcessBuilder(command); final Process p = pb.start(); final InputStream pIn = p.getInputStream(); final byte[] buffer = new byte[2048]; int exitCode = 0; for (;;) { if (pIn.available() > 0) { pIn.read(buffer); } try { exitCode = p.exitValue(); } catch (final IllegalThreadStateException itse) { continue; } break; } if (exitCode == 0) { final Reader r = new InputStreamReader(new FileInputStream(tmpOut), encoding); try { final StringBuilder sb = new StringBuilder(); for (;;) { final int c = r.read(); if (c >= 0) { sb.append((char) c); } else { break; } } return sb.toString(); } finally { r.close(); } } throw new IOException("Exited with exit code: " + exitCode); } finally { tmpIn.delete(); tmpOut.delete(); } }
From source file:org.cryptomator.frontend.webdav.mount.MacOsXAppleScriptWebDavMounter.java
private static void waitForProcessAndCheckSuccess(Process proc, long timeout, TimeUnit unit) throws CommandFailedException, IOException { try {/*from ww w . j a v a 2s . c o m*/ boolean finishedInTime = proc.waitFor(timeout, unit); if (!finishedInTime) { proc.destroyForcibly(); throw new CommandFailedException("Command timed out."); } int exitCode = proc.exitValue(); if (exitCode != 0) { String error = IOUtils.toString(proc.getErrorStream(), StandardCharsets.UTF_8); throw new CommandFailedException( "Command failed with exit code " + exitCode + ". Stderr: " + error); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } }
From source file:com.almarsoft.GroundhogReader.lib.FSUtils.java
public static boolean deleteDirectory(String directory) { Process process; try {// ww w.ja va 2s. c o m process = Runtime.getRuntime().exec("rm -r " + directory); process.waitFor(); if (process.exitValue() == 0) return true; } catch (IOException e) { Log.d("Groundhog", "IOException in rm -r " + directory); e.printStackTrace(); } catch (InterruptedException e) { Log.d("Groundhog", "InterruptedException in rm -r " + directory); e.printStackTrace(); } return false; }