ShellUtils.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. package cn.exlive.monitor.utils;
  2. import cn.exlive.monitor.config.ProgramsConfig;
  3. import org.apache.commons.exec.CommandLine;
  4. import org.apache.commons.exec.DefaultExecuteResultHandler;
  5. import org.apache.commons.exec.DefaultExecutor;
  6. import org.apache.commons.exec.ExecuteResultHandler;
  7. import org.apache.commons.exec.ExecuteWatchdog;
  8. import org.apache.commons.exec.ProcessDestroyer;
  9. import org.apache.commons.exec.PumpStreamHandler;
  10. import org.apache.commons.lang3.StringUtils;
  11. import org.apache.commons.lang3.SystemUtils;
  12. import org.apache.commons.lang3.exception.ExceptionUtils;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import org.springframework.boot.system.ApplicationHome;
  16. import java.io.BufferedInputStream;
  17. import java.io.BufferedReader;
  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.io.InputStreamReader;
  21. import java.io.Reader;
  22. import java.time.Duration;
  23. import java.util.ArrayList;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Objects;
  28. import java.util.Optional;
  29. import java.util.concurrent.CompletableFuture;
  30. import java.util.concurrent.TimeUnit;
  31. /**
  32. * 便捷得 Shell 执行工具类
  33. *
  34. * <pre>
  35. *
  36. * Created by zhaopx.
  37. * Date: 2025/6/9
  38. * Time: 12:00
  39. * Vendor: exlive.cn
  40. *
  41. * </pre>
  42. *
  43. * @author zhaopx
  44. */
  45. public class ShellUtils {
  46. private static final Logger logger = LoggerFactory.getLogger(ShellUtils.class);
  47. public static String INSTALL_PATH = getInstallPath();
  48. public final static String OS = SystemUtils.IS_OS_LINUX ? "linux" : "windows";
  49. private static ProcessBuilder processBuilder = new ProcessBuilder();
  50. /**
  51. * 返回 App HOME
  52. * @return
  53. */
  54. private static String getInstallPath() {
  55. String appHome = System.getenv("APP_HOME");
  56. if(StringUtils.isBlank(appHome)) {
  57. appHome = System.getProperty("app.home");
  58. }
  59. if(StringUtils.isBlank(appHome)) {
  60. // appHome = "D:/Work/Deploy";
  61. ApplicationHome ah = new ApplicationHome(ShellUtils.class);
  62. // 获取jar包所在目录
  63. File source = ah.getSource();
  64. File dir = ah.getDir();
  65. if(source == null && dir == null) {
  66. throw new IllegalStateException("请设置 Monitor APP_HOME 环境变量。");
  67. }
  68. if(source == null && dir != null) {
  69. appHome = dir.getParentFile().getAbsolutePath();
  70. } else {
  71. File appHomeFile = Optional.ofNullable(source.getParentFile()).map(File::getParentFile).orElseGet(()->{
  72. String userDir = System.getProperty("user.dir");
  73. return new File(org.springframework.util.StringUtils.hasLength(userDir) ? userDir : ".");
  74. });
  75. appHome = appHomeFile.getAbsolutePath();
  76. }
  77. logger.warn("use app home: {} recommen user set APP_HOME envirment.", appHome);
  78. }
  79. // monitor app home 的上一层是 INSTALL_DIR
  80. File homeDir = new File(appHome);
  81. String installPath = homeDir.getParentFile().getAbsolutePath();
  82. logger.info("INSTALL_PATH: {}", installPath);
  83. File dataDir = new File(homeDir, "data");
  84. if(!dataDir.exists()) {
  85. dataDir.mkdir();
  86. logger.info("mkdir monitor data dir: {}", dataDir.getAbsolutePath());
  87. }
  88. return installPath;
  89. }
  90. /**
  91. * @param pathOrCommand 脚本路径或者命令
  92. * @return
  93. */
  94. public static ExecResult exceShell(String pathOrCommand) {
  95. ExecResult result = new ExecResult();
  96. StringBuffer stringBuffer = new StringBuffer();
  97. try {
  98. // 执行脚本
  99. Process ps = Runtime.getRuntime().exec(new String[]{"sh", "-c", pathOrCommand});
  100. // 只能接收脚本echo打印的数据,并且是echo打印的最后一次数据
  101. BufferedInputStream in = new BufferedInputStream(ps.getInputStream());
  102. BufferedReader br = new BufferedReader(new InputStreamReader(in));
  103. String line;
  104. while ((line = br.readLine()) != null) {
  105. stringBuffer.append(line);
  106. stringBuffer.append(System.lineSeparator());
  107. }
  108. in.close();
  109. br.close();
  110. String execOut = stringBuffer.toString();
  111. int exitValue = ps.waitFor();
  112. result.setReturnCode(exitValue);
  113. if (0 == exitValue) {
  114. logger.info("{} command exec out is : {} {}", pathOrCommand, System.lineSeparator(), execOut);
  115. result.setExecResult(true);
  116. result.setExecOut(execOut);
  117. } else {
  118. result.setExecOut("call shell failed. error code is :" + exitValue);
  119. logger.error("{} command exec out is : {} {}", pathOrCommand, System.lineSeparator(), execOut);
  120. }
  121. } catch (Exception e) {
  122. result.setExecOut(e.getMessage());
  123. e.printStackTrace();
  124. }
  125. return result;
  126. }
  127. // 获取cpu架构 arm或x86
  128. public static String getCpuArchitecture() {
  129. try {
  130. Process ps = Runtime.getRuntime().exec("arch");
  131. StringBuffer stringBuffer = new StringBuffer();
  132. int exitValue = ps.waitFor();
  133. if (0 == exitValue) {
  134. // 只能接收脚本echo打印的数据,并且是echo打印的最后一次数据
  135. BufferedInputStream in = new BufferedInputStream(ps.getInputStream());
  136. BufferedReader br = new BufferedReader(new InputStreamReader(in));
  137. String line;
  138. while ((line = br.readLine()) != null) {
  139. logger.info("脚本返回的数据如下: " + line);
  140. stringBuffer.append(line);
  141. }
  142. in.close();
  143. br.close();
  144. return stringBuffer.toString();
  145. }
  146. } catch (Exception e) {
  147. e.printStackTrace();
  148. }
  149. return null;
  150. }
  151. public static ExecResult execWithStatus(String workPath, List<String> command, long timeout) {
  152. Process process = null;
  153. ExecResult result = new ExecResult();
  154. try {
  155. processBuilder.directory(new File(workPath));
  156. processBuilder.environment();
  157. processBuilder.command(command);
  158. processBuilder.redirectErrorStream(true);
  159. process = processBuilder.start();
  160. printOutput(process);
  161. boolean execResult = process.waitFor(timeout, TimeUnit.SECONDS);
  162. result.setReturnCode(process.exitValue());
  163. if (execResult && process.exitValue() == 0) {
  164. logger.info("script execute success");
  165. result.setExecResult(true);
  166. result.setExecOut("script execute success");
  167. } else {
  168. result.setExecOut("script execute failed");
  169. }
  170. return result;
  171. } catch (Exception e) {
  172. result.setExecErrOut(e.getMessage());
  173. e.printStackTrace();
  174. }
  175. return result;
  176. }
  177. public static ExecResult execWithLogs(String workPath, List<String> command, long timeout) {
  178. Process process = null;
  179. ExecResult result = new ExecResult();
  180. try {
  181. processBuilder.directory(new File(workPath));
  182. processBuilder.environment();
  183. processBuilder.command(command);
  184. processBuilder.redirectErrorStream(true);
  185. process = processBuilder.start();
  186. String output = getOutput(process);
  187. boolean execResult = process.waitFor(timeout, TimeUnit.SECONDS);
  188. result.setReturnCode(process.exitValue());
  189. result.setExecOut(output);
  190. if (execResult && process.exitValue() == 0) {
  191. logger.info("script execute success");
  192. result.setExecResult(true);
  193. } else {
  194. }
  195. return result;
  196. } catch (Exception e) {
  197. result.setExecErrOut(e.getMessage());
  198. e.printStackTrace();
  199. }
  200. return result;
  201. }
  202. public static ExecResult execWithStatus(String workPath, List<String> command, long timeout, Logger logger) {
  203. Process process = null;
  204. ExecResult result = new ExecResult();
  205. try {
  206. processBuilder.directory(new File(workPath));
  207. processBuilder.environment();
  208. processBuilder.command(command);
  209. processBuilder.redirectErrorStream(true);
  210. process = processBuilder.start();
  211. getOutput(process, logger);
  212. boolean execResult = process.waitFor(timeout, TimeUnit.SECONDS);
  213. result.setReturnCode(process.exitValue());
  214. if (execResult && process.exitValue() == 0) {
  215. logger.info("script execute success");
  216. result.setExecResult(true);
  217. result.setExecOut("script execute success");
  218. } else {
  219. result.setExecOut("script execute failed");
  220. }
  221. return result;
  222. } catch (Exception e) {
  223. result.setExecErrOut(e.getMessage());
  224. e.printStackTrace();
  225. }
  226. return result;
  227. }
  228. public static void getOutput(Process process, Logger logger) {
  229. CompletableFuture.runAsync(() -> {
  230. BufferedReader inReader = null;
  231. try {
  232. inReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
  233. String line;
  234. StringBuffer stringBuffer = new StringBuffer();
  235. while ((line = inReader.readLine()) != null) {
  236. stringBuffer.append(line);
  237. stringBuffer.append(System.lineSeparator());
  238. }
  239. logger.info(stringBuffer.toString());
  240. } catch (Exception e) {
  241. logger.error(e.getMessage(), e);
  242. } finally {
  243. closeQuietly(inReader);
  244. }
  245. BufferedReader errorReader = null;
  246. try {
  247. errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
  248. String line;
  249. StringBuffer stringBuffer = new StringBuffer();
  250. while ((line = errorReader.readLine()) != null) {
  251. stringBuffer.append(line);
  252. stringBuffer.append(System.lineSeparator());
  253. }
  254. logger.error(stringBuffer.toString());
  255. } catch (Exception e) {
  256. logger.error(e.getMessage(), e);
  257. } finally {
  258. closeQuietly(errorReader);
  259. }
  260. });
  261. }
  262. public static String getOutput(Process process) {
  263. StringBuffer stringBuffer = new StringBuffer();
  264. BufferedReader inReader = null;
  265. try {
  266. inReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
  267. String line;
  268. while ((line = inReader.readLine()) != null) {
  269. stringBuffer.append(line);
  270. stringBuffer.append(System.lineSeparator());
  271. }
  272. } catch (Exception e) {
  273. logger.error(e.getMessage(), e);
  274. } finally {
  275. closeQuietly(inReader);
  276. }
  277. return stringBuffer.toString();
  278. }
  279. public static void printOutput(Process process) {
  280. CompletableFuture.runAsync(() -> {
  281. BufferedReader inReader = null;
  282. try {
  283. inReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
  284. String line;
  285. StringBuffer stringBuffer = new StringBuffer();
  286. while ((line = inReader.readLine()) != null) {
  287. stringBuffer.append(line);
  288. stringBuffer.append(System.lineSeparator());
  289. }
  290. logger.trace(stringBuffer.toString());
  291. } catch (Exception e) {
  292. logger.error(e.getMessage(), e);
  293. } finally {
  294. closeQuietly(inReader);
  295. }
  296. });
  297. }
  298. public static String getError(Process process) {
  299. String errput = null;
  300. BufferedReader reader = null;
  301. try {
  302. if (process != null) {
  303. StringBuffer stringBuffer = new StringBuffer();
  304. reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
  305. while (reader.read() != -1) {
  306. stringBuffer.append("\n" + reader.readLine());
  307. }
  308. errput = stringBuffer.toString();
  309. }
  310. } catch (Exception e) {
  311. e.printStackTrace();
  312. }
  313. closeQuietly(reader);
  314. return errput;
  315. }
  316. public static ExecResult exec(String commands) {
  317. return exec(null, CommandLine.parse(commands), new HashMap<>(), -1, null);
  318. }
  319. public static ExecResult exec(String commands, int timeoutSecond) {
  320. return exec(null, CommandLine.parse(commands), new HashMap<>(), timeoutSecond, null);
  321. }
  322. public static ExecResult exec(String workPath, String commands) {
  323. return exec(workPath, CommandLine.parse(commands), new HashMap<>(), -1, null);
  324. }
  325. public static ExecResult exec(String workPath, String commands, int timeoutSecond) {
  326. return exec(workPath, CommandLine.parse( commands), new HashMap<>(), timeoutSecond, null);
  327. }
  328. /**
  329. * 执行命令,并返回回调信息
  330. * @param workPath
  331. * @param commands
  332. * @param timeoutSecond
  333. * @param executeResultHandler
  334. * @return
  335. */
  336. public static ExecResult exec(String workPath, String commands, int timeoutSecond, BrodcastExecuteResultHandler executeResultHandler) {
  337. return exec(workPath, CommandLine.parse( commands), new HashMap<>(), timeoutSecond, executeResultHandler);
  338. }
  339. public static ExecResult exec(String workPath, String commands, Map<String, String> envs, int timeoutSecond) {
  340. return exec(workPath, CommandLine.parse( commands), envs, timeoutSecond, null);
  341. }
  342. /**
  343. * 执行命令,并返回回调信息
  344. * @param workPath
  345. * @param commands
  346. * @param timeoutSecond
  347. * @param executeResultHandler
  348. * @return
  349. */
  350. public static ExecResult exec(String workPath, String commands, Map<String, String> envs, int timeoutSecond, BrodcastExecuteResultHandler executeResultHandler) {
  351. return exec(workPath, CommandLine.parse( commands), envs, timeoutSecond, executeResultHandler);
  352. }
  353. public static ExecResult exec(List<String> commands) {
  354. return exec(null, CommandLine.parse(StringUtils.join(commands, " ")), new HashMap<>(), -1, null);
  355. }
  356. public static ExecResult exec(List<String> commands, int timeoutSecond) {
  357. return exec(null, CommandLine.parse(StringUtils.join(commands, " ")), new HashMap<>(), timeoutSecond, null);
  358. }
  359. public static ExecResult exec(String workPath, List<String> commands) {
  360. return exec(workPath, CommandLine.parse(StringUtils.join(commands, " ")), new HashMap<>(), -1, null);
  361. }
  362. public static ExecResult exec(String workPath, List<String> commands, int timeoutSecond) {
  363. return exec(workPath, CommandLine.parse(StringUtils.join(commands, " ")), new HashMap<>(), timeoutSecond, null);
  364. }
  365. /**
  366. * 带有回调的执行命令
  367. * @param workPath
  368. * @param commands
  369. * @param timeoutSecond
  370. * @param executeResultHandler
  371. * @return
  372. */
  373. public static ExecResult exec(String workPath, List<String> commands, int timeoutSecond, BrodcastExecuteResultHandler executeResultHandler) {
  374. return exec(workPath, CommandLine.parse(StringUtils.join(commands, " ")), new HashMap<>(), timeoutSecond, null);
  375. }
  376. public static ExecResult exec(String workPath, CommandLine commandLine, Map<String, String> envs, int timeoutSecond, DefaultExecuteResultHandler handler) {
  377. DefaultExecutor executor = new DefaultExecutor();
  378. executor.setExitValue(-1);
  379. // handler 记录
  380. handler = (handler == null ? new DefaultExecuteResultHandler() : handler);
  381. ShellLogOutputStream output = new ShellLogOutputStream();
  382. ShellLogOutputStream err = new ShellLogOutputStream();
  383. executor.setStreamHandler(new PumpStreamHandler(output, err));
  384. PidExecuteWatchdog watchDog = null;
  385. // 不限时间
  386. if(timeoutSecond > 0) {
  387. watchDog = new PidExecuteWatchdog(Duration.ofSeconds(timeoutSecond).toMillis(), handler);
  388. executor.setWatchdog(watchDog);
  389. }
  390. String exeScript = "bash";
  391. if (SystemUtils.IS_OS_WINDOWS) {
  392. // windows 系统不需要执行,也没有权限
  393. exeScript = "";
  394. } else {
  395. // Linux, Mac 系统使用 bash 直接执行脚本
  396. exeScript = "bash";
  397. }
  398. // String cmd = StringUtils.join(commands, " ");
  399. if(StringUtils.isNotBlank(workPath)) {
  400. logger.info("from path: {} execute cmd: {}", workPath, commandLine);
  401. executor.setWorkingDirectory(new File(workPath));
  402. } else {
  403. logger.info("execute cmd: " + commandLine);
  404. }
  405. Map<String, String> environment = new HashMap<>(System.getenv());
  406. if(envs != null && !envs.isEmpty()) {
  407. environment.putAll(envs);
  408. }
  409. ExecResult result = new ExecResult();
  410. final long start = System.currentTimeMillis();
  411. try {
  412. executor.execute(commandLine, environment, handler);
  413. try {
  414. result.setPid(watchDog != null ? watchDog.getProcessPid() : -1);
  415. } catch (Exception ignore) {}
  416. if (timeoutSecond > 0) {
  417. handler.waitFor(Duration.ofSeconds(timeoutSecond));
  418. } else {
  419. handler.waitFor();
  420. }
  421. int exitCode = handler.getExitValue();
  422. result.setUseTime(System.currentTimeMillis() - start);
  423. result.setExecOut(output.toString());
  424. result.setReturnCode(exitCode);
  425. result.setExecErrOut(err.toString());
  426. result.setExecResult(Objects.equals(0, exitCode));
  427. return result;
  428. } catch (Exception e) {
  429. result.setUseTime(System.currentTimeMillis() - start);
  430. result.setExecResult(false);
  431. result.setReturnCode(-1);
  432. result.setExecOut(output.toString());
  433. result.setExecErrOut(ExceptionUtils.getRootCauseMessage(e));
  434. logger.error("execute: " + commandLine + " error.", e);
  435. return result;
  436. }
  437. }
  438. public static void closeQuietly(Reader reader) {
  439. try {
  440. if (reader != null) {
  441. reader.close();
  442. }
  443. } catch (IOException ioe) {
  444. ioe.printStackTrace();
  445. }
  446. }
  447. public static void destroy(Process process) {
  448. if (process != null) {
  449. process.destroyForcibly();
  450. }
  451. }
  452. public static void addChmod(String path, String chmod) {
  453. ArrayList<String> command = new ArrayList<>();
  454. command.add("chmod");
  455. command.add("-R");
  456. command.add(chmod);
  457. command.add(path);
  458. execWithStatus(INSTALL_PATH, command, 60, logger);
  459. }
  460. public static void addChown(String path, String user, String group) {
  461. ArrayList<String> command = new ArrayList<>();
  462. command.add("chown");
  463. command.add("-R");
  464. command.add(user + ":" + group);
  465. command.add(path);
  466. execWithStatus(INSTALL_PATH, command, 60, logger);
  467. }
  468. }