com.feingto.cloud.kit.ProcessKit Maven / Gradle / Ivy
package com.feingto.cloud.kit;
import lombok.extern.slf4j.Slf4j;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Objects;
/**
* Process 工具类
*
* @author longfei
*/
@Slf4j
public class ProcessKit {
/**
* 执行脚本命令
*
* @param cmd 命令字符串
*/
public static void execCmd(String cmd) {
String os = System.getProperties().getProperty("os.name");
log.debug(os);
Process process;
try {
if (Objects.nonNull(os) && (os.toLowerCase().contains("linux") || os.toLowerCase().contains("mac"))) {
log.debug(cmd);
process = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", cmd});
} else {
log.debug(cmd);
process = Runtime.getRuntime().exec("cmd /c docker " + cmd);
}
process.waitFor();
BufferedReader read = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while (Objects.nonNull(line = read.readLine())) {
log.debug(line);
}
log.debug("命令" + cmd + "执行完毕");
} catch (Exception e) {
throw new RuntimeException("命令" + cmd + "执行失败");
}
}
}