| 
					
				 | 
			
			
				@@ -4,8 +4,11 @@ import org.slf4j.Logger; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.slf4j.LoggerFactory; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import java.io.*; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.nio.charset.StandardCharsets; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import java.util.Arrays; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import java.util.List; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.concurrent.ExecutorService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.concurrent.Executors; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  * Linux命令执行工具 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -22,21 +25,8 @@ public class LinuxCommand { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				      */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     public static void exec(String cmd) throws Exception { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         Process process = Runtime.getRuntime().exec(cmd); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            log.info("要执行命令了哟:{}", cmd); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            print(process); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            int code = process.waitFor(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            log.info("{}命令,执行状态{}", cmd, code); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (code != 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                throw new Exception("执行命令出错啦!" + cmd); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } catch (Exception e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            throw e; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } finally { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            process.destroy(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        log.info("要执行命令了哟:{}", cmd); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        handleProcess(process); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     /** 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -46,21 +36,8 @@ public class LinuxCommand { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				      */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     public static void exec(String[] cmd) throws Exception { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         Process process = Runtime.getRuntime().exec(cmd); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            log.info("要执行命令了哟:{}", Arrays.toString(cmd)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            print(process); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            int code = process.waitFor(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            log.info("{}命令,执行状态{}", cmd, code); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (code != 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                throw new Exception("执行命令出错啦!" + cmd); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } catch (Exception e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            throw e; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } finally { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            process.destroy(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        log.info("要执行命令了哟:{}", Arrays.toString(cmd)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        handleProcess(process); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -79,18 +56,8 @@ public class LinuxCommand { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         processBuilder.redirectOutput(new File(path)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         Process process = processBuilder.start(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            print(process); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            int code = process.waitFor(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            log.info("mysql备份执行状态:{}", code); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (code != 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                throw new Exception("执行mysql备份命令出错啦!"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } catch (Exception e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            throw e; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } finally { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            process.destroy(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        handleProcess(process); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     /** 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -103,19 +70,7 @@ public class LinuxCommand { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         log.info("要执行命令了哟:{}", Arrays.toString(commands.toArray())); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         ProcessBuilder processBuilder = new ProcessBuilder(commands); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         Process process = processBuilder.start(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            print(process); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            int code = process.waitFor(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            log.info("命令执行状态:{}", code); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (code != 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                throw new Exception("执行命令出错啦!"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } catch (Exception e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            throw e; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } finally { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            process.destroy(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        handleProcess(process); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     /** 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -132,68 +87,49 @@ public class LinuxCommand { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         processBuilder.directory(new File(dir)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         Process process = processBuilder.start(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            print(process); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            int code = process.waitFor(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            log.info("脚本执行状态:{}", code); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (code != 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                throw new Exception("执行脚本出错啦!"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        handleProcess(process); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private static void handleProcess(Process process) throws Exception { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        try (InputStream inputStream = process.getInputStream(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+             OutputStream outputStream = process.getOutputStream(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+             PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)), true); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+             InputStream errorStream = process.getErrorStream()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            pw.println("exit"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            // 异步读取子进程的标准输出和错误输出 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            ExecutorService executor = Executors.newFixedThreadPool(2); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            executor.submit(() -> readAndLog(inputStream, "子进程标准输出:")); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            executor.submit(() -> readAndLog(errorStream, "子进程错误输出:")); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            int exitCode = process.waitFor(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            log.info("命令执行状态:{}", exitCode); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (exitCode != 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                throw new Exception("执行命令或脚本出错!"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } catch (Exception e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            throw e; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            executor.shutdown(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } finally { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             process.destroy(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-     * 打印执行日志 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-     * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-     * @param process 进程 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    private static void print(Process process) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // 获取子进程的输入流 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        InputStream inputStream = process.getInputStream(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // 获取子进程的错误流 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        InputStream errorStream = process.getErrorStream(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // 获取子进程的输出流 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        OutputStream outputStream = process.getOutputStream(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        try (PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)), true);) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            pw.println("exit"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } catch (Exception e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            log.error("出错啦:{}", e.getMessage()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        //新起线程读取输入流 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        new Thread(() -> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            try (BufferedReader readInput = new BufferedReader(new InputStreamReader(inputStream))) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                String line; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                log.info("子进程输入流:"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                while ((line = readInput.readLine()) != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    log.info(line); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } catch (IOException e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                log.error("出错啦:{}", e.getMessage()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        }).start(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        //新开线程读取错误流 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        new Thread(() -> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            try (BufferedReader readError = new BufferedReader(new InputStreamReader(errorStream))) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                String line; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                log.info("子进程错误流:"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                while ((line = readError.readLine()) != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    log.info(line); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } catch (IOException e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                log.error("出错啦:{}", e.getMessage()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private static void readAndLog(InputStream inputStream, String prefix) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            String line; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            while ((line = reader.readLine()) != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                log.info("{} {}", prefix, line); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        }).start(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } catch (IOException e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            log.error("{}读取时出错:{}", prefix, e.getMessage()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     public static void main(String[] args) throws Exception { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        String[] cmds = {"ipconfig", "/c", "clear"}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        String cmd = "ipconfig"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        LinuxCommand.exec(cmds); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        for (int i = 0; i < 10000; i++) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            String[] cmds = {"ipconfig", "/all"}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            String cmd = "ipconfig"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            System.err.println("----------------------------" + i); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            LinuxCommand.exec(cmds); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 |