java shell 超时_Java执行Shell脚本超时控制

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

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);

}

}

}

}

}

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2009-07-01 20:16

浏览 7818

评论

3 楼

u013246812

2017-06-20

谢谢博主帮我解决了问题,就是那个process.exitValue()方法是解决问题的关键,我一直没注意点这个方法的执行时抛异常表示子进程还没有执行完。

2 楼

lee79

2009-11-13

呵呵,讲的很对

966903dea4bcb507358d5dcce8b912e5.gif

1 楼

fangwei

2009-08-17

非常感谢!!!

btw 你虽然用到了slf4j,却没有用到它的强项

logger.info("Execute command [{}], last [{}] s.",command,last);

如果是遗留问题,可以当我废话

75a3bf8974c9bd5a56863a745113d139.gif

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值