package com.fdkankan.scene.util; import lombok.extern.slf4j.Slf4j; /** * Created by Xiewj on 2021/1/4 0004 14:53 */ @Slf4j public class CmdUtils { /** * 调用算法 xx.sh 脚本 * * @param command */ // public static void callshell(String command){ // try { // String[] cmd = new String[]{"/bin/sh", "-c", command}; // Process process = Runtime.getRuntime().exec(cmd); // StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), "ERROR"); // errorGobbler.start(); // StreamGobbler outGobbler = new StreamGobbler(process.getInputStream(), "STDOUT"); // outGobbler.start(); // process.waitFor(); // } catch (Exception e) { // e.printStackTrace(); // } // } static void callLineSh(String command) { CmdUtils.callLineSh(command, null); } /** * 直接java调用命令 * * @param command */ static void callLine(String command) { CmdUtils.callLine(command, null); } /** * 直接java调用命令 * * @param command */ static void callLineWin(String command) { CmdUtils.callLineWin(command, null); } /** * 直接java调用命令 * * @param command */ private static void callLineWin(String command, Integer lineSize) { CmdUtils.log.info("cmd: " + command); try { String[] cmd = new String[]{"cmd ", "/c", command}; Process process = Runtime.getRuntime().exec(cmd); CmdUtils.log.info("开始运行"); StreamGobblerLine errorGobbler = new StreamGobblerLine(process.getErrorStream(), "ERROR"); errorGobbler.start(); // 200行打印一次日志 StreamGobblerLine outGobbler = new StreamGobblerLine(process.getInputStream(), "STDOUT", lineSize); outGobbler.start(); process.waitFor(); } catch (Exception e) { e.printStackTrace(); } } /** * 直接java调用命令 * * @param command */ private static void callLine(String command, Integer lineSize) { CmdUtils.log.info("cmd: " + command); try { Process process = Runtime.getRuntime().exec(command); CmdUtils.log.info("开始运行"); StreamGobblerLine errorGobbler = new StreamGobblerLine(process.getErrorStream(), "ERROR"); errorGobbler.start(); // 200行打印一次日志 StreamGobblerLine outGobbler = new StreamGobblerLine(process.getInputStream(), "STDOUT", lineSize); outGobbler.start(); process.waitFor(); } catch (Exception e) { e.printStackTrace(); } } /** * @param command 命令 * @param lineSize 日志输出行数 ,可以为null */ private static void callLineSh(String command, Integer lineSize) { CmdUtils.log.info("cmd: " + command); try { String[] cmd = new String[]{"/bin/sh", "-c", command}; Process process = Runtime.getRuntime().exec(cmd); CmdUtils.log.info("开始运行"); StreamGobblerLine errorGobbler = new StreamGobblerLine(process.getErrorStream(), "ERROR"); errorGobbler.start(); // 200行打印一次日志 StreamGobblerLine outGobbler = new StreamGobblerLine(process.getInputStream(), "STDOUT", lineSize); outGobbler.start(); process.waitFor(); } catch (Exception e) { e.printStackTrace(); } } /** * 调用sh脚本上传oss */ // public static void ossUploadDir(String sceneCode, String uploadDir, String target){ // // String cmd = CmdConstant.OSSUTIL_UPLOAD_DIR; // cmd = cmd.replaceAll("@sceneCode", sceneCode); // cmd = cmd.replaceAll("@uploadDir", uploadDir); // cmd = cmd.replaceAll("@target", target); // // log.info("ossCmd: " + cmd); // long start = System.currentTimeMillis(); // CmdUtils.callLineSh(cmd); // long end = System.currentTimeMillis(); // log.info("场景码目录:{} 上传完成, 耗时:{} s" , sceneCode, (end-start)/1000 ); // } /** * 调用sh脚本上传oss */ public static void ossUploadDir(String cmd) { // log.info("ossCmd: " + cmd); long start = System.currentTimeMillis(); CmdUtils.callLineSh(cmd); long end = System.currentTimeMillis(); CmdUtils.log.info("场景码目录:{} 上传完成, 耗时:{} s", (end - start) / 1000); } public static void ossDeleteDir(String cmd) { CmdUtils.log.info("ossCmd: " + cmd); long start = System.currentTimeMillis(); CmdUtils.callLineSh(cmd); long end = System.currentTimeMillis(); CmdUtils.log.info("删除目录完成, 耗时:{} s", (end - start) / 1000); } }