public class Exec {
private static ILogger logger = LoggerFactory.getLogger(Exec.class);
public Exec() {
super();
}
? ? /**
? ? ?* 執行命令(如Shell腳本)<br>
? ? ?*?
? ? ?* @param cmd 操作命令
? ? ?* @param timeout 超時時間
? ? ?* @return 命令執行過程輸出內容
? ? ?*?
? ? ?* @throws IOException
? ? ?* @throws TimeoutException
? ? ?* @throws InterruptedException
? ? ?*/
? ? public String execute(final String cmd, final long timeout) throws IOException, TimeoutException, InterruptedException{
? ? if(cmd == null || cmd.trim().length() == 0) {
? ? return null;
? ? }
? ? long time = 1000L * 5;
? ? time = timeout > 0 ? timeout : time;
? ? ? ??
? ? long begin = System.currentTimeMillis();
? ?
? ? logger.info("Begin execute command { " + cmd + " }, timeout = " + time + " seconds.");
? ? ? ??
? ? //執行命令
? ? Process process = Runtime.getRuntime().exec(cmd);?
? ? ? ??
? ? ? ? TimeWorker timeWorker = new TimeWorker(process);
? ? ? ? process.getOutputStream().close();
? ? ? ? OutputWorker outputWorker = new OutputWorker(process.getInputStream());
? ? ? ? String result = null;
? ? ? ? outputWorker.start();
? ? ? ? timeWorker.start();
? ? ? ? try {
? ? ? ?
? ? ? ? //任務超時
? ? ? ? ? ? timeWorker.join(time);
? ? ? ? ? ? if (timeWorker.exit == null) {
? ? ? ? ? ? ? ? throw new TimeoutException(cmd);
? ? ? ? ? ? } else if(timeWorker.exit != 0) {
? ? ? ? ? ? logger.error("Execute command " + cmd + " error, error message: " + this.getOutput(process.getErrorStream()));
? ? ? ? ? ? ? ? result = "error";
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? result = outputWorker.output;
? ? ? ? ? ? }
? ? ? ? } catch (InterruptedException e) {
? ? ? ? ? ? timeWorker.interrupt();
? ? ? ? ? ? Thread.currentThread().interrupt();
? ? ? ? ? ? throw e;
? ? ? ? } finally {
? ? ? ? ? ? process.destroy();
? ? ? ? }
? ? ? ??
? ? ? ? long end = System.currentTimeMillis();
? ? ? ? logger.info("Finish execute command { " + cmd + " }, time spent " + (end - begin)/1000L + " seconds.");
? ?
? ? return result;
? ? }
? ??
? ? /**
? ? ?* 獲取IO輸出內容 <br>
? ? ?*?
? ? ?* @param stream 流
? ? ?* @return 內容
? ? ?* @throws IOException
? ? ?*/
? ? private String getOutput(InputStream stream) throws IOException {
? ? if(stream == null) {
? ? return null;
? ? }
? ? ? ? StringBuffer buffer = new StringBuffer();
? ? ? ? BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
? ? ? ? String line = null;
? ? ? ? while((line = reader.readLine()) != null) {
? ? ? ? ? ? buffer.append(line + "\n");
? ? ? ? }
? ? ? ? reader.close();
? ? ? ? return buffer.toString().trim();
? ? }
? ??
? ? /**
? ? ?*?
? ? ?* @author?
? ? ?*
? ? ?*/
? ? private static class OutputWorker extends Thread {
? ?
? ? ? ? private final InputStream stream;
? ? ? ? private String output = null;
? ? ? ? private OutputWorker(InputStream stream) {
? ? ? ? ? ? this.stream = stream;
? ? ? ? }
? ? ? ??
? ? ? ? public void ?run() {
? ? ? ? ? ? String line = null;
? ? ? ? ? ? StringBuffer buffer=new StringBuffer();
? ? ? ? ? ? BufferedReader reader=null;
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? reader = new BufferedReader(new InputStreamReader(stream));
? ? ? ? ? ? ? ? while((line = reader.readLine()) != null) {
? ? ? ? ? ? ? ? ? ? buffer.append(line+"\n");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? this.output = buffer.toString().trim();
? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? logger.error(e.getMessage());
? ? ? ? ? ? } finally {
? ? ? ? ? ? ? ? if(reader!=null) {
? ? ? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? ? ? reader.close();
? ? ? ? ? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? ? ? logger.error(e.getMessage());
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ??
? ? /**
? ? ?*?
? ? ?* @author
? ? ?*
? ? ?*/
? ? private static class TimeWorker extends Thread {
? ?
? ? ? ? private final Process process;
? ? ? ??
? ? ? ? private Integer exit = null;
? ? ? ? private TimeWorker(Process process) {
? ? ? ? ? ? this.process = process;
? ? ? ? }
? ? ? ??
? ? ? ? public void run() {
? ? ? ? ? ? try {
? ? ? ? ? ? //阻塞等待命令執行結束
? ? ? ? ? ? ? ? exit = process.waitFor();
? ? ? ? ? ? } catch (InterruptedException e) {
? ? ? ? ? ? logger.error(e.getMessage());
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
private static ILogger logger = LoggerFactory.getLogger(Exec.class);
public Exec() {
super();
}
? ? /**
? ? ?* 執行命令(如Shell腳本)<br>
? ? ?*?
? ? ?* @param cmd 操作命令
? ? ?* @param timeout 超時時間
? ? ?* @return 命令執行過程輸出內容
? ? ?*?
? ? ?* @throws IOException
? ? ?* @throws TimeoutException
? ? ?* @throws InterruptedException
? ? ?*/
? ? public String execute(final String cmd, final long timeout) throws IOException, TimeoutException, InterruptedException{
? ? if(cmd == null || cmd.trim().length() == 0) {
? ? return null;
? ? }
? ? long time = 1000L * 5;
? ? time = timeout > 0 ? timeout : time;
? ? ? ??
? ? long begin = System.currentTimeMillis();
? ?
? ? logger.info("Begin execute command { " + cmd + " }, timeout = " + time + " seconds.");
? ? ? ??
? ? //執行命令
? ? Process process = Runtime.getRuntime().exec(cmd);?
? ? ? ??
? ? ? ? TimeWorker timeWorker = new TimeWorker(process);
? ? ? ? process.getOutputStream().close();
? ? ? ? OutputWorker outputWorker = new OutputWorker(process.getInputStream());
? ? ? ? String result = null;
? ? ? ? outputWorker.start();
? ? ? ? timeWorker.start();
? ? ? ? try {
? ? ? ?
? ? ? ? //任務超時
? ? ? ? ? ? timeWorker.join(time);
? ? ? ? ? ? if (timeWorker.exit == null) {
? ? ? ? ? ? ? ? throw new TimeoutException(cmd);
? ? ? ? ? ? } else if(timeWorker.exit != 0) {
? ? ? ? ? ? logger.error("Execute command " + cmd + " error, error message: " + this.getOutput(process.getErrorStream()));
? ? ? ? ? ? ? ? result = "error";
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? result = outputWorker.output;
? ? ? ? ? ? }
? ? ? ? } catch (InterruptedException e) {
? ? ? ? ? ? timeWorker.interrupt();
? ? ? ? ? ? Thread.currentThread().interrupt();
? ? ? ? ? ? throw e;
? ? ? ? } finally {
? ? ? ? ? ? process.destroy();
? ? ? ? }
? ? ? ??
? ? ? ? long end = System.currentTimeMillis();
? ? ? ? logger.info("Finish execute command { " + cmd + " }, time spent " + (end - begin)/1000L + " seconds.");
? ?
? ? return result;
? ? }
? ??
? ? /**
? ? ?* 獲取IO輸出內容 <br>
? ? ?*?
? ? ?* @param stream 流
? ? ?* @return 內容
? ? ?* @throws IOException
? ? ?*/
? ? private String getOutput(InputStream stream) throws IOException {
? ? if(stream == null) {
? ? return null;
? ? }
? ? ? ? StringBuffer buffer = new StringBuffer();
? ? ? ? BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
? ? ? ? String line = null;
? ? ? ? while((line = reader.readLine()) != null) {
? ? ? ? ? ? buffer.append(line + "\n");
? ? ? ? }
? ? ? ? reader.close();
? ? ? ? return buffer.toString().trim();
? ? }
? ??
? ? /**
? ? ?*?
? ? ?* @author?
? ? ?*
? ? ?*/
? ? private static class OutputWorker extends Thread {
? ?
? ? ? ? private final InputStream stream;
? ? ? ? private String output = null;
? ? ? ? private OutputWorker(InputStream stream) {
? ? ? ? ? ? this.stream = stream;
? ? ? ? }
? ? ? ??
? ? ? ? public void ?run() {
? ? ? ? ? ? String line = null;
? ? ? ? ? ? StringBuffer buffer=new StringBuffer();
? ? ? ? ? ? BufferedReader reader=null;
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? reader = new BufferedReader(new InputStreamReader(stream));
? ? ? ? ? ? ? ? while((line = reader.readLine()) != null) {
? ? ? ? ? ? ? ? ? ? buffer.append(line+"\n");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? this.output = buffer.toString().trim();
? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? logger.error(e.getMessage());
? ? ? ? ? ? } finally {
? ? ? ? ? ? ? ? if(reader!=null) {
? ? ? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? ? ? reader.close();
? ? ? ? ? ? ? ? ? ? } catch (IOException e) {
? ? ? ? ? ? ? ? ? ? logger.error(e.getMessage());
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ??
? ? /**
? ? ?*?
? ? ?* @author
? ? ?*
? ? ?*/
? ? private static class TimeWorker extends Thread {
? ?
? ? ? ? private final Process process;
? ? ? ??
? ? ? ? private Integer exit = null;
? ? ? ? private TimeWorker(Process process) {
? ? ? ? ? ? this.process = process;
? ? ? ? }
? ? ? ??
? ? ? ? public void run() {
? ? ? ? ? ? try {
? ? ? ? ? ? //阻塞等待命令執行結束
? ? ? ? ? ? ? ? exit = process.waitFor();
? ? ? ? ? ? } catch (InterruptedException e) {
? ? ? ? ? ? logger.error(e.getMessage());
? ? ? ? ? ? }
? ? ? ? }
? ? }
}