public static int runProcess(String[] pstr, int timeout) throws TimeoutException, InterruptedException, IOException { String cmdStr = ""; for (String st : pstr) { cmdStr = cmdStr + " " + st; } SDFSLogger.getLog().debug("Executing [" + cmdStr + "]"); Process p = null; try { p = Runtime.getRuntime().exec(pstr, null, new File(Main.volume.getPath())); ReadStream s1 = new ReadStream("stdin", p.getInputStream()); ReadStream s2 = new ReadStream("stderr", p.getErrorStream()); s1.start(); s2.start(); } catch (Throwable e) { SDFSLogger.getLog().error("unable to execute " + cmdStr, e); throw new IOException(e); } long now = System.currentTimeMillis(); long finish = now + timeout; while (isAlive(p) && (System.currentTimeMillis() < finish)) { Thread.sleep(10); } if (isAlive(p)) { throw new TimeoutException("Process [" + cmdStr + "] timeout out after [" + timeout + "] ms"); } return p.exitValue(); }
public static int runProcess(String pstr) throws TimeoutException, InterruptedException, IOException { SDFSLogger.getLog().debug("Executing [" + pstr + "]"); Process p = null; try { p = Runtime.getRuntime().exec(pstr, null, new File(Main.volume.getPath())); ReadStream s1 = new ReadStream("stdin", p.getInputStream()); ReadStream s2 = new ReadStream("stderr", p.getErrorStream()); s1.start(); s2.start(); } catch (Throwable e) { SDFSLogger.getLog().error("unable to execute " + pstr, e); throw new IOException(e); } p.waitFor(); return p.exitValue(); }