Skip to content

Commit 7e609a2

Browse files
committed
fix[command]: process can not end with input stream
1 parent f217246 commit 7e609a2

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

monitor/src/main/java/com/zfoo/monitor/util/OSUtils.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@
3434
import java.util.ArrayList;
3535
import java.util.HashMap;
3636
import java.util.List;
37-
import java.util.Map;
38-
import java.util.concurrent.ConcurrentHashMap;
39-
import java.util.stream.Collectors;
37+
import java.util.concurrent.TimeUnit;
4038

4139
/**
4240
* Oshi库封装的工具类,通过此工具类,可获取系统、硬件相关信息
@@ -281,24 +279,26 @@ public static String doExecCommand(String command, File wd) {
281279
.directory(wd)
282280
.start();
283281

282+
var finished = process.waitFor(256, TimeUnit.SECONDS);
283+
if (!finished) {
284+
process.destroyForcibly();
285+
throw new RunException("doExecCommand timeout with process of command:[{}]", command);
286+
}
287+
284288
//取得命令结果的输出流
285289
inputStream = process.getInputStream();
286290
var bytes = IOUtils.toByteArray(inputStream);
287291
var result = StringUtils.bytesToString(bytes);
288292

289-
// 其他线程都等待这个线程完成
290-
process.waitFor();
291-
// 获取javac线程的退出值,0代表正常退出,非0代表异常中止
293+
// 获取线程的退出值,0代表正常退出,非0代表异常中止
292294
int exitValue = process.exitValue();
293-
294-
// 返回编译是否成功
295295
if (exitValue != 0) {
296-
throw new RunException("error executing command exitValue:[{}] result:[{}]", exitValue, result);
296+
throw new RunException("doExecCommand error executing command exitValue:[{}] result:[{}]", exitValue, result);
297297
}
298298

299299
return result;
300300
} catch (Exception e) {
301-
logger.error("unknown exception in command execution", e);
301+
logger.error("doExecCommand unknown exception in command execution", e);
302302
} finally {
303303
if (process != null) {
304304
process.destroy();

0 commit comments

Comments
 (0)