停服脚本日志输出修改

master
duhui 2021-08-16 18:02:32 +08:00
parent aabe4cea6e
commit 91cbdeebf9
1 changed files with 36 additions and 3 deletions

View File

@ -10,6 +10,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.*;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
@ -158,10 +159,8 @@ public class AutoServerManager {
exec = Runtime.getRuntime().exec(command);
LOGGER.info("========================== 停服脚本执行结果开始 ===========================");
new DealProcessSream(exec.getInputStream(),true).start();
new DealProcessSream(exec.getErrorStream(),false).start();
readProcessOutput(exec);
LOGGER.info("=========================== 停服脚本执行结果结束 ==========================");
exec.waitFor();
} catch (Exception e) {
e.printStackTrace();
@ -171,4 +170,38 @@ public class AutoServerManager {
}
}
}
/**
*
* @param process
*/
private static void readProcessOutput(final Process process) {
// 将进程的正常输出在 System.out 中打印,进程的错误输出在 System.err 中打印
read(process.getInputStream(), System.out);
read(process.getErrorStream(), System.err);
}
/**
*
* @param inputStream
* @param out
*/
private static void read(InputStream inputStream, PrintStream out) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}