Java執行Shell指令碼超時控制
阿新 • • 發佈:2019-02-01
Java的Runtime可以執行命令列指令碼,某些特定場合需要對指令碼的執行時間進行控制,防止指令碼某些異常情況下,一直未能正確結束,導致Java主程序掛起。本文的程式對這一過程進行了控制
Java程式碼- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- publicclass CommandUtils {
- private
- //default time out, in millseconds
- publicstaticfinalint DEFAULT_TIMEOUT = 10 * 1000;
- publicstaticfinalint DEFAULT_INTERVAL = 1000;
- /**
- * Executes the specified command in a separate process. The method then blocks until the process returned.
- * If an error arises during the execution or if the exeecuted process returned an non-null return code,
- * the content of the process' stderr is returned to the caller. If the execution is fine, null is returned.
- *
- * @param command String
- * @return CommandResult
- */
- public
- long start = System.currentTimeMillis();
- long last;
- CommandResult commandResult = new CommandResult();
- try {
- Process process = Runtime.getRuntime().exec(command);
- process(process, commandResult);
- if (process != null) {
- process.destroy();
- }
- last = (System.currentTimeMillis() - start) / 1000;
- logger.info("Execute command [" + command + "], last [" + last + "] s.");
- } catch (Exception e) {
- last = (System.currentTimeMillis() - start) / 1000;
- String error = "Execute command [" + command + "] last [" + last + "] s, failed [" + e.getMessage() + "]";
- logger.error(error, e);
- commandResult.setExitValue(CommandResult.EXIT_VALUE_UNEXCEPTED);
- commandResult.setErrorOutput(error);
- }
- return commandResult;
- }
- privatestaticvoid process(Process process, CommandResult commandResult) {
- BufferedReader errorReader = null;
- BufferedReader inputReader = null;
- try {
- errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
- inputReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
- //timeout control
- long start = System.currentTimeMillis();
- boolean processFinished = false;
- while (System.currentTimeMillis() - start < DEFAULT_TIMEOUT && !processFinished) {
- processFinished = true;
- try {
- process.exitValue();
- } catch (IllegalThreadStateException e) {
- // process hasn't finished yet
- processFinished = false;
- try {
- Thread.sleep(DEFAULT_INTERVAL);
- } catch (InterruptedException e1) {
- logger.error("Process, failed [" + e.getMessage() + "]", e);
- }
- }
- }
- if (!processFinished) {
- commandResult.setExitValue(CommandResult.EXIT_VALUE_TIMEOUT);
- commandResult.setErrorOutput("Command process timeout");
- return;
- }
- commandResult.setExitValue(process.waitFor());
- StringBuffer sb;
- String line;
- //parse error info
- if (errorReader.ready()) {
- sb = new StringBuffer();
- while ((line = errorReader.readLine()) != null) {
- sb.append(line);
- }
- commandResult.setErrorOutput(sb.toString());
- }
- //parse info
- if (inputReader.ready()) {
- sb = new StringBuffer();
- while ((line = inputReader.readLine()) != null) {
- sb.append(line);
- }
- commandResult.setInfoOutput(sb.toString());
- }
- } catch (Exception e) {
- String error = "Command process, failed [" + e.getMessage() + "]";
- logger.error(error, e);
- commandResult.setExitValue(CommandResult.EXIT_VALUE_UNEXCEPTED);
- commandResult.setErrorOutput(error);
- } finally {
- if (errorReader != null) {
- try {
- errorReader.close();
- } catch (IOException e) {
- logger.error("Close BufferedReader, failed [" + e.getMessage() + "]", e);
- }
- }
- if (inputReader != null) {
- try {
- inputReader.close();
- } catch (IOException e) {
- logger.error("Close BufferedReader, failed [" + e.getMessage() + "]", e);
- }
- }
- }
- }
- }
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CommandUtils {
private static Logger logger = LoggerFactory.getLogger(CommandUtils.class);
//default time out, in millseconds
public static final int DEFAULT_TIMEOUT = 10 * 1000;
public static final int DEFAULT_INTERVAL = 1000;
/**
* Executes the specified command in a separate process. The method then blocks until the process returned.
* If an error arises during the execution or if the exeecuted process returned an non-null return code,
* the content of the process' stderr is returned to the caller. If the execution is fine, null is returned.
*
* @param command String
* @return CommandResult
*/
public static CommandResult exec(String command) {
long start = System.currentTimeMillis();
long last;
CommandResult commandResult = new CommandResult();
try {
Process process = Runtime.getRuntime().exec(command);
process(process, commandResult);
if (process != null) {
process.destroy();
}
last = (System.currentTimeMillis() - start) / 1000;
logger.info("Execute command [" + command + "], last [" + last + "] s.");
} catch (Exception e) {
last = (System.currentTimeMillis() - start) / 1000;
String error = "Execute command [" + command + "] last [" + last + "] s, failed [" + e.getMessage() + "]";
logger.error(error, e);
commandResult.setExitValue(CommandResult.EXIT_VALUE_UNEXCEPTED);
commandResult.setErrorOutput(error);
}
return commandResult;
}
private static void process(Process process, CommandResult commandResult) {
BufferedReader errorReader = null;
BufferedReader inputReader = null;
try {
errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
inputReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
//timeout control
long start = System.currentTimeMillis();
boolean processFinished = false;
while (System.currentTimeMillis() - start < DEFAULT_TIMEOUT && !processFinished) {
processFinished = true;
try {
process.exitValue();
} catch (IllegalThreadStateException e) {
// process hasn't finished yet
processFinished = false;
try {
Thread.sleep(DEFAULT_INTERVAL);
} catch (InterruptedException e1) {
logger.error("Process, failed [" + e.getMessage() + "]", e);
}
}
}
if (!processFinished) {
commandResult.setExitValue(CommandResult.EXIT_VALUE_TIMEOUT);
commandResult.setErrorOutput("Command process timeout");
return;
}
commandResult.setExitValue(process.waitFor());
StringBuffer sb;
String line;
//parse error info
if (errorReader.ready()) {
sb = new StringBuffer();
while ((line = errorReader.readLine()) != null) {
sb.append(line);
}
commandResult.setErrorOutput(sb.toString());
}
//parse info
if (inputReader.ready()) {
sb = new StringBuffer();
while ((line = inputReader.readLine()) != null) {
sb.append(line);
}
commandResult.setInfoOutput(sb.toString());
}
} catch (Exception e) {
String error = "Command process, failed [" + e.getMessage() + "]";
logger.error(error, e);
commandResult.setExitValue(CommandResult.EXIT_VALUE_UNEXCEPTED);
commandResult.setErrorOutput(error);
} finally {
if (errorReader != null) {
try {
errorReader.close();
} catch (IOException e) {
logger.error("Close BufferedReader, failed [" + e.getMessage() + "]", e);
}
}
if (inputReader != null) {
try {
inputReader.close();
} catch (IOException e) {
logger.error("Close BufferedReader, failed [" + e.getMessage() + "]", e);
}
}
}
}
}