1、
2、
import org.jeecg.zhongyi.auto_dep.util.CommandStreamGobbler;
import org.jeecg.zhongyi.util.LogbackUtil;
import org.jeecg.zhongyi.util.vo.Result;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class auto_dep_helper {
public static Result dep_env_wait(String cmd_sh) {
Process process = null;
List<String> commandList = new LinkedList<>();
//commandList.add("/data/xxxxx/admin_xx_auto_deployment.sh");
commandList.add(cmd_sh);
commandList.add("hello world ");
String[] commands = new String[commandList.size()];
for (int i = 0; i < commandList.size(); i++) {
commands[i] = commandList.get(i);
}
InputStreamReader stdISR = null;
InputStreamReader errISR = null;
String logName = cmd_sh;
try {
process = Runtime.getRuntime().exec(commands);
CommandStreamGobbler errorGobbler = new CommandStreamGobbler(process.getErrorStream(), logName, "ERR");
CommandStreamGobbler outputGobbler = new CommandStreamGobbler(process.getInputStream(), logName, "STD");
errorGobbler.start();
// 必须先等待错误输出ready再建立标准输出
while (!errorGobbler.isReady()) {
Thread.sleep(10);
}
outputGobbler.start();
while (!outputGobbler.isReady()) {
Thread.sleep(10);
}
int exitValue = process.waitFor();
System.out.println("exitValue=" + exitValue);
if (exitValue == 0) {
LogbackUtil.getErrorLogger().info("更新部署成功");
return Result.success();
} else {
LogbackUtil.getErrorLogger().info("更新部署失败");
return Result.error();
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
LogbackUtil.getErrorLogger().error(e.getMessage(), e);
return Result.error(e.getMessage());
} finally {
try {
if (stdISR != null) {
stdISR.close();
}
if (errISR != null) {
errISR.close();
}
if (process != null) {
process.destroy();
}
} catch (IOException e) {
System.out.println("正式执行命令:" + logName + "有IO异常");
LogbackUtil.getErrorLogger().error(e.getMessage(), e);
return Result.error(e.getMessage());
}
}
}
public static Result dep_env_not_wait(String cmd_sh) {
Process process = null;
List<String> commandList = new LinkedList<>();
//commandList.add("/data/xxxxx/admin_xx_auto_deployment.sh");
commandList.add(cmd_sh);
commandList.add("hello world ");
String[] commands = new String[commandList.size()];
for (int i = 0; i < commandList.size(); i++) {
commands[i] = commandList.get(i);
}
InputStreamReader stdISR = null;
InputStreamReader errISR = null;
String logName = cmd_sh;
try {
process = Runtime.getRuntime().exec(commands);
CommandStreamGobbler errorGobbler = new CommandStreamGobbler(process.getErrorStream(), logName, "ERR");
CommandStreamGobbler outputGobbler = new CommandStreamGobbler(process.getInputStream(), logName, "STD");
errorGobbler.start();
// 必须先等待错误输出ready再建立标准输出
while (!errorGobbler.isReady()) {
Thread.sleep(10);
}
outputGobbler.start();
while (!outputGobbler.isReady()) {
Thread.sleep(10);
}
boolean exitValue = process.waitFor(5, TimeUnit.SECONDS);
System.out.println("exitValue=" + exitValue);
if (exitValue) {
LogbackUtil.getErrorLogger().info("更新部署成功,请稍等,并验证结果");
return Result.success("更新部署成功,请稍等,并验证结果");
} else {
LogbackUtil.getErrorLogger().info("更新部署失败,请稍等,并验证结果");
return Result.error("更新部署超时,请稍等,并验证结果");
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
LogbackUtil.getErrorLogger().error(e.getMessage(), e);
return Result.error(e.getMessage());
} finally {
try {
if (stdISR != null) {
stdISR.close();
}
if (errISR != null) {
errISR.close();
}
if (process != null) {
process.destroy();
}
} catch (IOException e) {
System.out.println("正式执行命令:" + logName + "有IO异常");
LogbackUtil.getErrorLogger().error(e.getMessage(), e);
return Result.error(e.getMessage());
}
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;
public class CommandStreamGobbler extends Thread {
private InputStream is;
private String command;
private String prefix = "";
private boolean readFinish = false;
private boolean ready = false;
private List<String> infoList = new LinkedList<String>();
public CommandStreamGobbler(InputStream is, String command, String prefix) {
this.is = is;
this.command = command;
this.prefix = prefix;
}
public void run() {
InputStreamReader isr = null;
try {
isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
ready = true;
while (br != null && (line = br.readLine()) != null) {
infoList.add(line);
System.out.println(prefix + " line: " + line);
LogbackUtil.getErrorLogger().info(prefix + " line: " + line);
}
} catch (IOException ioe) {
System.out.println("正式执行命令:" + command + "有IO异常");
LogbackUtil.getErrorLogger().error("正式执行命令:" + command + "有IO异常", ioe);
} finally {
try {
if (isr != null) {
isr.close();
}
} catch (IOException ioe) {
System.out.println("正式执行命令:" + command + "有IO异常");
LogbackUtil.getErrorLogger().error("正式执行命令:" + command + "有IO异常", ioe);
}
readFinish = true;
}
}
public InputStream getIs() {
return is;
}
public String getCommand() {
return command;
}
public boolean isReadFinish() {
return readFinish;
}
public boolean isReady() {
return ready;
}
public List<String> getInfoList() {
return infoList;
}
}
标签:java,String,process,commandList,return,sh,linux,import,null
From: https://www.cnblogs.com/kikyoqiang/p/17489806.html