|
@@ -4,6 +4,7 @@ import lombok.SneakyThrows;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import java.io.BufferedInputStream;
|
|
|
+import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
|
|
@@ -30,6 +31,36 @@ public class LinuxUtil {
|
|
|
return out.toString();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 指定工作目录执行目录
|
|
|
+ *
|
|
|
+ * @param command 命令
|
|
|
+ * @param directory 工作目录
|
|
|
+ * @return 执行结果
|
|
|
+ */
|
|
|
+ public static String execute(String command, String directory) {
|
|
|
+ try {
|
|
|
+ String result;
|
|
|
+ log.info("execute() 开始执行 linux 命令:" + command);
|
|
|
+ Runtime run = Runtime.getRuntime();
|
|
|
+ Process process = run.exec(command, null, new File(directory));
|
|
|
+ InputStream in = new BufferedInputStream(process.getInputStream());
|
|
|
+ StringBuilder out = new StringBuilder();
|
|
|
+ byte[] b = new byte[8192];
|
|
|
+ for (int n; (n = in.read(b)) != -1; ) {
|
|
|
+ out.append(new String(b, 0, n));
|
|
|
+ }
|
|
|
+ in.close();
|
|
|
+ process.destroy();
|
|
|
+ result = out.toString();
|
|
|
+ log.info("execute() 执行结果为:" + result);
|
|
|
+ return result;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public static void execute2(String cmd1, String cmd2) throws IOException, InterruptedException {
|
|
|
Runtime run = Runtime.getRuntime();
|
|
|
log.info("开始执行 linux 命令:" + cmd1);
|