CmdUtils.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package com.fdkankan.external.util;
  2. import lombok.extern.slf4j.Slf4j;
  3. /**
  4. * Created by Xiewj on 2021/1/4 0004 14:53
  5. */
  6. @Slf4j
  7. public class CmdUtils {
  8. /**
  9. * 调用算法 xx.sh 脚本
  10. * @param command
  11. */
  12. // public static void callshell(String command){
  13. // try {
  14. // String[] cmd = new String[]{"/bin/sh", "-c", command};
  15. // Process process = Runtime.getRuntime().exec(cmd);
  16. // StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), "ERROR");
  17. // errorGobbler.start();
  18. // StreamGobbler outGobbler = new StreamGobbler(process.getInputStream(), "STDOUT");
  19. // outGobbler.start();
  20. // process.waitFor();
  21. // } catch (Exception e) {
  22. // e.printStackTrace();
  23. // }
  24. // }
  25. public static void callLineSh(String command) throws Exception {
  26. callLineSh(command, null);
  27. }
  28. /**
  29. * 直接java调用命令
  30. * @param command
  31. */
  32. public static void callLine(String command){
  33. callLine(command, null);
  34. }
  35. /**
  36. * 直接java调用命令
  37. * @param command
  38. */
  39. public static void callLine(String command, Integer lineSize){
  40. log.info("cmd: " + command);
  41. try {
  42. Process process = Runtime.getRuntime().exec(command);
  43. log.info("开始运行");
  44. StreamGobblerLine errorGobbler = new StreamGobblerLine(process.getErrorStream(), "ERROR");
  45. errorGobbler.start();
  46. // 200行打印一次日志
  47. StreamGobblerLine outGobbler = new StreamGobblerLine(process.getInputStream(), "STDOUT", lineSize);
  48. outGobbler.start();
  49. process.waitFor();
  50. } catch (Exception e) {
  51. e.printStackTrace();
  52. }
  53. }
  54. /**
  55. *
  56. * @param command 命令
  57. * @param lineSize 日志输出行数 ,可以为null
  58. */
  59. public static void callLineSh(String command, Integer lineSize) throws Exception {
  60. log.info("cmd: " + command);
  61. String[] cmd = new String[]{"/bin/sh", "-c", command};
  62. Process process = Runtime.getRuntime().exec(cmd);
  63. log.info("开始运行");
  64. StreamGobblerLine errorGobbler = new StreamGobblerLine(process.getErrorStream(), "ERROR");
  65. errorGobbler.start();
  66. // 200行打印一次日志
  67. StreamGobblerLine outGobbler = new StreamGobblerLine(process.getInputStream(), "STDOUT", lineSize);
  68. outGobbler.start();
  69. process.waitFor();
  70. }
  71. /**
  72. * 调用sh脚本上传oss
  73. */
  74. // public static void ossUploadDir(String sceneCode, String uploadDir, String target){
  75. //
  76. // String cmd = CmdConstant.OSSUTIL_UPLOAD_DIR;
  77. // cmd = cmd.replaceAll("@sceneCode", sceneCode);
  78. // cmd = cmd.replaceAll("@uploadDir", uploadDir);
  79. // cmd = cmd.replaceAll("@target", target);
  80. //
  81. // log.info("ossCmd: " + cmd);
  82. // long start = System.currentTimeMillis();
  83. // CmdUtils.callLineSh(cmd);
  84. // long end = System.currentTimeMillis();
  85. // log.info("场景码目录:{} 上传完成, 耗时:{} s" , sceneCode, (end-start)/1000 );
  86. // }
  87. }