/** * Shell执行超时时间 */ private static final int SHELL_TIME_OUT = 10;
StringBuilder builder = new StringBuilder("删除指定文件.log");
String[] cmd = {"/bin/sh", "-c", "rm -rf "+ builder.toString()};
Scanner input = null;
String result = "";
Process process = null;
try {
process = Runtime.getRuntime().exec(cmd);
// 等待命令执行完成
process.waitFor(SHELL_TIME_OUT, TimeUnit.SECONDS);
log.info(" Shell Execute Begin Cmd rm -rf {}", builder.toString());
InputStream is = process.getInputStream();
input = new Scanner(is);
while (input.hasNextLine()) {
result += input.nextLine() + "\n";
}
log.info(" Shell Execute Result {}", result);
} catch (InterruptedException | IOException e) {
e.printStackTrace();
log.error(" Shell Execute Meets An Error {}", e.getMessage());
} finally {
if (null != input) {
input.close();
}
if (null != process) {
process.destroy();
}
}