CmdUtils.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package com.fdkankan.scene.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. *
  11. * @param command
  12. */
  13. // public static void callshell(String command){
  14. // try {
  15. // String[] cmd = new String[]{"/bin/sh", "-c", command};
  16. // Process process = Runtime.getRuntime().exec(cmd);
  17. // StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), "ERROR");
  18. // errorGobbler.start();
  19. // StreamGobbler outGobbler = new StreamGobbler(process.getInputStream(), "STDOUT");
  20. // outGobbler.start();
  21. // process.waitFor();
  22. // } catch (Exception e) {
  23. // e.printStackTrace();
  24. // }
  25. // }
  26. static void callLineSh(String command) {
  27. CmdUtils.callLineSh(command, null);
  28. }
  29. /**
  30. * 直接java调用命令
  31. *
  32. * @param command
  33. */
  34. static void callLine(String command) {
  35. CmdUtils.callLine(command, null);
  36. }
  37. /**
  38. * 直接java调用命令
  39. *
  40. * @param command
  41. */
  42. static void callLineWin(String command) {
  43. CmdUtils.callLineWin(command, null);
  44. }
  45. /**
  46. * 直接java调用命令
  47. *
  48. * @param command
  49. */
  50. private static void callLineWin(String command, Integer lineSize) {
  51. CmdUtils.log.info("cmd: " + command);
  52. try {
  53. String[] cmd = new String[]{"cmd ", "/c", command};
  54. Process process = Runtime.getRuntime().exec(cmd);
  55. CmdUtils.log.info("开始运行");
  56. StreamGobblerLine errorGobbler = new StreamGobblerLine(process.getErrorStream(), "ERROR");
  57. errorGobbler.start();
  58. // 200行打印一次日志
  59. StreamGobblerLine outGobbler = new StreamGobblerLine(process.getInputStream(), "STDOUT", lineSize);
  60. outGobbler.start();
  61. process.waitFor();
  62. } catch (Exception e) {
  63. e.printStackTrace();
  64. }
  65. }
  66. /**
  67. * 直接java调用命令
  68. *
  69. * @param command
  70. */
  71. private static void callLine(String command, Integer lineSize) {
  72. CmdUtils.log.info("cmd: " + command);
  73. try {
  74. Process process = Runtime.getRuntime().exec(command);
  75. CmdUtils.log.info("开始运行");
  76. StreamGobblerLine errorGobbler = new StreamGobblerLine(process.getErrorStream(), "ERROR");
  77. errorGobbler.start();
  78. // 200行打印一次日志
  79. StreamGobblerLine outGobbler = new StreamGobblerLine(process.getInputStream(), "STDOUT", lineSize);
  80. outGobbler.start();
  81. process.waitFor();
  82. } catch (Exception e) {
  83. e.printStackTrace();
  84. }
  85. }
  86. /**
  87. * @param command 命令
  88. * @param lineSize 日志输出行数 ,可以为null
  89. */
  90. private static void callLineSh(String command, Integer lineSize) {
  91. CmdUtils.log.info("cmd: " + command);
  92. try {
  93. String[] cmd = new String[]{"/bin/sh", "-c", command};
  94. Process process = Runtime.getRuntime().exec(cmd);
  95. CmdUtils.log.info("开始运行");
  96. StreamGobblerLine errorGobbler = new StreamGobblerLine(process.getErrorStream(), "ERROR");
  97. errorGobbler.start();
  98. // 200行打印一次日志
  99. StreamGobblerLine outGobbler = new StreamGobblerLine(process.getInputStream(), "STDOUT", lineSize);
  100. outGobbler.start();
  101. process.waitFor();
  102. } catch (Exception e) {
  103. e.printStackTrace();
  104. }
  105. }
  106. /**
  107. * 调用sh脚本上传oss
  108. */
  109. // public static void ossUploadDir(String sceneCode, String uploadDir, String target){
  110. //
  111. // String cmd = CmdConstant.OSSUTIL_UPLOAD_DIR;
  112. // cmd = cmd.replaceAll("@sceneCode", sceneCode);
  113. // cmd = cmd.replaceAll("@uploadDir", uploadDir);
  114. // cmd = cmd.replaceAll("@target", target);
  115. //
  116. // log.info("ossCmd: " + cmd);
  117. // long start = System.currentTimeMillis();
  118. // CmdUtils.callLineSh(cmd);
  119. // long end = System.currentTimeMillis();
  120. // log.info("场景码目录:{} 上传完成, 耗时:{} s" , sceneCode, (end-start)/1000 );
  121. // }
  122. /**
  123. * 调用sh脚本上传oss
  124. */
  125. public static void ossUploadDir(String cmd) {
  126. // log.info("ossCmd: " + cmd);
  127. long start = System.currentTimeMillis();
  128. CmdUtils.callLineSh(cmd);
  129. long end = System.currentTimeMillis();
  130. CmdUtils.log.info("场景码目录:{} 上传完成, 耗时:{} s", (end - start) / 1000);
  131. }
  132. public static void ossDeleteDir(String cmd) {
  133. CmdUtils.log.info("ossCmd: " + cmd);
  134. long start = System.currentTimeMillis();
  135. CmdUtils.callLineSh(cmd);
  136. long end = System.currentTimeMillis();
  137. CmdUtils.log.info("删除目录完成, 耗时:{} s", (end - start) / 1000);
  138. }
  139. }