root 2 年之前
父節點
當前提交
96c2b844eb

+ 31 - 0
api-common/src/main/java/api/common/util/LinuxUtil.java

@@ -4,6 +4,7 @@ import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
 
 import java.io.BufferedInputStream;
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 
@@ -30,6 +31,36 @@ public class LinuxUtil {
         return out.toString();
     }
 
+    /**
+     * 指定工作目录执行目录
+     *
+     * @param command   命令
+     * @param directory 工作目录
+     * @return 执行结果
+     */
+    public static String execute(String command, String directory) {
+        try {
+            String result;
+            log.info("execute() 开始执行 linux 命令:" + command);
+            Runtime run = Runtime.getRuntime();
+            Process process = run.exec(command, null, new File(directory));
+            InputStream in = new BufferedInputStream(process.getInputStream());
+            StringBuilder out = new StringBuilder();
+            byte[] b = new byte[8192];
+            for (int n; (n = in.read(b)) != -1; ) {
+                out.append(new String(b, 0, n));
+            }
+            in.close();
+            process.destroy();
+            result = out.toString();
+            log.info("execute() 执行结果为:" + result);
+            return result;
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new RuntimeException(e);
+        }
+    }
+
     public static void execute2(String cmd1, String cmd2) throws IOException, InterruptedException {
         Runtime run = Runtime.getRuntime();
         log.info("开始执行 linux 命令:" + cmd1);

+ 6 - 2
simulation-resource-server/src/main/java/com/css/simulation/resource/scene/service/SceneGeneralExampleService.java

@@ -162,9 +162,13 @@ public class SceneGeneralExampleService {
                 .setRedirectsEnabled(false)
                 .setExpectContinueEnabled(false)
                 .build();
-        log.info("saveSceneGeneralExample()调用泛化接口--" + generalUrl + "--入参:" + map);
+        log.info("saveSceneGeneralExample() 调用泛化接口--" + generalUrl + "--入参:" + map);
+        String execute = LinuxUtil.execute("ps -ef | grep main_web.py");
+        if (!execute.contains("main_web.py")) {
+            LinuxUtil.execute("./service-manager.sh restart", "/root/");
+        }
         String post = HttpUtil.post(HttpUtil.getHttpClient(), requestConfig, generalUrl, null, map);
-        log.info("saveSceneGeneralExample()调用泛化接口--" + generalUrl + "--出参:" + post);
+        log.info("saveSceneGeneralExample() 调用泛化接口--" + generalUrl + "--出参:" + post);
 
         boolean success = JsonUtil.readTree(post).get("success").asBoolean();
         if (!success) {