ShellUtils.java
package com.wc.toolTest.util; import java.io.*; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.stream.Stream; public class ShellUtils { static String shell = Utils.isWindows() ? "cmd" : "/bin/sh"; //"/bin/sh"; static String shellArg = Utils.isWindows() ? "/c" : "-c"; //-c public static String exec(String command) { Process process = null; String str = null; try { process = Runtime.getRuntime().exec(command); BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream())); Stream<String> lines = input.lines(); str = ""; String line = ""; while ((str = input.readLine()) != null){ str += line; } input.close(); int exitVal = process.exitValue(); } catch (IOException e) { e.printStackTrace(); } return str; } /** * 运行shell脚本 * @param shell 需要运行的shell脚本 */ public static void execShell(String shell){ try { Runtime.getRuntime().exec(shell); } catch (Exception e) { e.printStackTrace(); } } /** * 运行shell脚本 new String[]方式 * @param shell 需要运行的shell脚本 */ public static void execShellBin(String shell){ try { Runtime.getRuntime().exec(new String[]{shell, shellArg,shell},null,null); } catch (Exception e) { e.printStackTrace(); } } /** * 运行shell并获得结果,注意:如果sh中含有awk,一定要按new String[]{"/bin/sh","-c",shStr}写,才可以获得流 * * @param shStr * 需要执行的shell * @return */ public static String runShell(String shStr) { String strList = null; try { Process process = Runtime.getRuntime().exec(new String[]{shell, shellArg,shStr},null,null); process.waitFor(); InputStreamReader ir = new InputStreamReader(process.getInputStream()); LineNumberReader input = new LineNumberReader(ir); String line; strList = ""; while ((line = input.readLine()) != null){ strList += line + "\n"; } } catch (Exception e) { e.printStackTrace(); } return strList; } public static void main(String[] args){ //Process process = Runtime.getRuntime().exec("cmd.exe /c dir"); //int status = process.waitFor(); // //System.out.println(status); //InputStream in = process.getInputStream(); // //BufferedReader br = new BufferedReader(new InputStreamReader(in)); //String line = br.readLine(); //while(line!=null) { //System.out.println(line); //line = br.readLine(); //} System.out.print(ShellUtils.runShell("curl ifconfig.me")); } }
TimingRunShell.java
package com.wc; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.UnsupportedEncodingException; public class TimingRunShell { public static void main(String[] args) { TimingRunShell main = new TimingRunShell(); main.start(); } private void start(){ System.out.println("selfPath:" + getSelfPath()); String waitTime = readTxtToString(getSelfPath() + "waitTime.txt").trim(); String []arrShellPath = readTxtToString(getSelfPath() + "shellPath.txt").trim().split("\n"); Long w = Long.valueOf(waitTime); while(true){ for(String shellPath : arrShellPath){ shellPath = shellPath.trim(); if(shellPath.isEmpty()){ continue; } System.out.println("running:" + shellPath); ShellUtils.execShellBin(shellPath); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } try { Thread.sleep(w * 1000L); } catch (InterruptedException e) { e.printStackTrace(); } } } public String getSelfPath() { String path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath(); if(System.getProperty("os.name").contains("dows")) { path = path.substring(1,path.length()); } if(path.contains("jar")) { path = path.substring(0,path.lastIndexOf(".")); return path.substring(0,path.lastIndexOf("/")); } return path.replace("target/classes/", ""); } public String readTxtToString(String fileName) { String encoding = "UTF-8"; File file = new File(fileName); Long filelength = file.length(); byte[] filecontent = new byte[filelength.intValue()]; try { FileInputStream in = new FileInputStream(file); in.read(filecontent); in.close(); } catch (FileNotFoundException e) { e.printStackTrace(); return null; } catch (IOException e) { e.printStackTrace(); return null; } try { return new String(filecontent, encoding); } catch (UnsupportedEncodingException e) { System.err.println("The OS does not support " + encoding); e.printStackTrace(); return null; } } }