Java执行Shell脚本超时控制

 

Java的Runtime可以执行命令行脚本,某些特定场合需要对脚本的执行时间进行控制,防止脚本某些异常情况下,一直未能正确结束,导致Java主进程挂起。本文的程序对这一过程进行了控制

 

Java代码
  1. import org.slf4j.Logger;   
  2. import org.slf4j.LoggerFactory;   
  3.   
  4. import java.io.BufferedReader;   
  5. import java.io.IOException;   
  6. import java.io.InputStreamReader;   
  7.   
  8.     
  9.   
  10. public class CommandUtils {   
  11.     private static Logger logger = LoggerFactory.getLogger(CommandUtils.class);   
  12.   
  13.     //default time out, in millseconds   
  14.     public static final int DEFAULT_TIMEOUT = 10 * 1000;   
  15.     public static final int DEFAULT_INTERVAL = 1000;   
  16.   
  17.     /**  
  18.      * Executes the specified command in a separate process. The method then blocks until the process returned.  
  19.      * If an error arises during the execution or if the exeecuted process returned an non-null return code,  
  20.      * the content of the process' stderr is returned to the caller. If the execution is fine, null is returned.  
  21.      *  
  22.      * @param command String  
  23.      * @return CommandResult  
  24.      */  
  25.     public static CommandResult exec(String command) {   
  26.         long start = System.currentTimeMillis();   
  27.         long last;   
  28.   
  29.         CommandResult commandResult = new CommandResult();   
  30.   
  31.         try {   
  32.             Process process = Runtime.getRuntime().exec(command);   
  33.             process(process, commandResult);   
  34.   
  35.             if (process != null) {   
  36.                 process.destroy();   
  37.             }   
  38.   
  39.             last = (System.currentTimeMillis() - start) / 1000;   
  40.             logger.info("Execute command [" + command + "], last [" + last + "] s.");   
  41.   
  42.         } catch (Exception e) {   
  43.             last = (System.currentTimeMillis() - start) / 1000;   
  44.             String error = "Execute command [" + command + "] last [" + last + "] s, failed [" + e.getMessage() + "]";   
  45.             logger.error(error, e);   
  46.   
  47.             commandResult.setExitValue(CommandResult.EXIT_VALUE_UNEXCEPTED);   
  48.             commandResult.setErrorOutput(error);   
  49.         }   
  50.   
  51.         return commandResult;   
  52.     }   
  53.   
  54.     private static void process(Process process, CommandResult commandResult) {   
  55.         BufferedReader errorReader = null;   
  56.         BufferedReader inputReader = null;   
  57.   
  58.         try {   
  59.             errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));   
  60.             inputReader = new BufferedReader(new InputStreamReader(process.getInputStream()));   
  61.   
  62.             //timeout control   
  63.             long start = System.currentTimeMillis();   
  64.             boolean processFinished = false;   
  65.   
  66.             while (System.currentTimeMillis() - start < DEFAULT_TIMEOUT && !processFinished) {   
  67.                 processFinished = true;   
  68.                 try {   
  69.                     process.exitValue();   
  70.                 } catch (IllegalThreadStateException e) {   
  71.                     // process hasn't finished yet   
  72.                     processFinished = false;   
  73.   
  74.                     try {   
  75.                         Thread.sleep(DEFAULT_INTERVAL);   
  76.                     } catch (InterruptedException e1) {   
  77.                         logger.error("Process, failed [" + e.getMessage() + "]", e);   
  78.                     }   
  79.                 }   
  80.             }   
  81.   
  82.             if (!processFinished) {   
  83.                 commandResult.setExitValue(CommandResult.EXIT_VALUE_TIMEOUT);   
  84.                 commandResult.setErrorOutput("Command process timeout");   
  85.                 return;   
  86.             }   
  87.   
  88.             commandResult.setExitValue(process.waitFor());   
  89.   
  90.             StringBuffer sb;   
  91.             String line;   
  92.   
  93.             //parse error info   
  94.             if (errorReader.ready()) {   
  95.                 sb = new StringBuffer();   
  96.                 while ((line = errorReader.readLine()) != null) {   
  97.                     sb.append(line);   
  98.                 }   
  99.                 commandResult.setErrorOutput(sb.toString());   
  100.             }   
  101.   
  102.             //parse info   
  103.             if (inputReader.ready()) {   
  104.                 sb = new StringBuffer();   
  105.                 while ((line = inputReader.readLine()) != null) {   
  106.                     sb.append(line);   
  107.                 }   
  108.                 commandResult.setInfoOutput(sb.toString());   
  109.             }   
  110.   
  111.         } catch (Exception e) {   
  112.             String error = "Command process, failed [" + e.getMessage() + "]";   
  113.             logger.error(error, e);   
  114.   
  115.             commandResult.setExitValue(CommandResult.EXIT_VALUE_UNEXCEPTED);   
  116.             commandResult.setErrorOutput(error);   
  117.   
  118.         } finally {   
  119.             if (errorReader != null) {   
  120.                 try {   
  121.                     errorReader.close();   
  122.                 } catch (IOException e) {   
  123.                     logger.error("Close BufferedReader, failed [" + e.getMessage() + "]", e);   
  124.                 }   
  125.             }   
  126.   
  127.             if (inputReader != null) {   
  128.                 try {   
  129.                     inputReader.close();   
  130.                 } catch (IOException e) {   
  131.                     logger.error("Close BufferedReader, failed [" + e.getMessage() + "]", e);   
  132.                 }   
  133.             }   
  134.         }   
  135.     }   
  136. }  

 

你可能感兴趣的:(java,exception,shell,脚本,command,null)