|
@@ -22,9 +22,28 @@ public class LinuxCommand {
|
|
|
public static void exec(String cmd) throws Exception {
|
|
|
Process process = Runtime.getRuntime().exec(cmd);
|
|
|
print(process);
|
|
|
- log.info("{}命令,执行状态{}", cmd, process);
|
|
|
|
|
|
- if (process.waitFor() != 0) {
|
|
|
+ int code = process.waitFor();
|
|
|
+ log.info("{}命令,执行状态{}", cmd, code);
|
|
|
+
|
|
|
+ if (code != 0) {
|
|
|
+ throw new Exception("执行命令出错啦!" + cmd);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量执行linux命令
|
|
|
+ *
|
|
|
+ * @param cmd
|
|
|
+ */
|
|
|
+ public static void exec(String[] cmd) throws Exception {
|
|
|
+ Process process = Runtime.getRuntime().exec(cmd);
|
|
|
+ print(process);
|
|
|
+
|
|
|
+ int code = process.waitFor();
|
|
|
+ log.info("{}命令,执行状态{}", cmd, code);
|
|
|
+
|
|
|
+ if (code != 0) {
|
|
|
throw new Exception("执行命令出错啦!" + cmd);
|
|
|
}
|
|
|
}
|
|
@@ -58,7 +77,7 @@ public class LinuxCommand {
|
|
|
/**
|
|
|
* 执行mysql导出命令
|
|
|
*
|
|
|
- * @param commands 命令
|
|
|
+ * @param commands 命令分段,必须要分割,不能直接拼好一条add进来会报错,commands中的String不需要加空格
|
|
|
* @param path 导出文件路径
|
|
|
* @throws Exception 异常说明
|
|
|
*/
|
|
@@ -70,10 +89,36 @@ public class LinuxCommand {
|
|
|
|
|
|
print(process);
|
|
|
|
|
|
- log.info("mysql备份执行状态:{}", process.waitFor());
|
|
|
- if (process.waitFor() != 0) {
|
|
|
+ int code = process.waitFor();
|
|
|
+ log.info("mysql备份执行状态:{}", code);
|
|
|
+ if (code != 0) {
|
|
|
+ throw new Exception("执行mysql备份命令出错啦!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 执行mysql导出命令
|
|
|
+ *
|
|
|
+ * @param commands 命令分段,必须要分割,不能直接拼好一条add进来会报错,commands中的String不需要加空格
|
|
|
+ * @throws Exception 异常说明
|
|
|
+ */
|
|
|
+ public static void mysqlImport(List<String> commands) throws Exception {
|
|
|
+ ProcessBuilder processBuilder = new ProcessBuilder(commands);
|
|
|
+ Process process = processBuilder.start();
|
|
|
+
|
|
|
+ print(process);
|
|
|
+
|
|
|
+ int code = process.waitFor();
|
|
|
+ log.info("mysql备份执行状态:{}", code);
|
|
|
+ if (code != 0) {
|
|
|
throw new Exception("执行mysql备份命令出错啦!");
|
|
|
}
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+ String[] cmds = {"ipconfig", "/c", "clear"};
|
|
|
+ String cmd = "ipconfig";
|
|
|
+ LinuxCommand.exec(cmds);
|
|
|
}
|
|
|
}
|