1
zj
2025-07-09 b0628cd6949a915596461719d16ba20b59dff73b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package org.example.controller;
 
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.example.WsServerApplication;
import org.example.common.ServerResponse;
import org.example.pojo.User;
import org.example.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
 
@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "*")
@Slf4j
public class RunController {
 
    @Autowired
    private ConfigurableApplicationContext context;
 
    @PostMapping("/restart")
    @ResponseBody
    public ServerResponse restart(HttpServletRequest request) {
        String token = request.getHeader("token");
        User user = JwtUtil.verify(token);
        if(user.getIsRoot() != 1){
            return ServerResponse.createByErrorMsg("没有重启权限");
        }
        Thread restartThread = new Thread(() -> {
            try {
                Thread.sleep(1000); // 等待1秒钟,确保接口返回成功响应
                SpringApplication.exit(context, () -> 0);
                SpringApplication.run(WsServerApplication.class); // 替换成你的Spring Boot主类
            } catch (Exception e) {
                e.printStackTrace();
                log.error("重启失败");
            }
        });
        restartThread.setDaemon(false);
        restartThread.start();
        return ServerResponse.createBySuccess("重启成功");
    }
 
 
    @PostMapping("/resetScript")
    @ResponseBody
    public ServerResponse resetScript(HttpServletRequest request,@RequestParam("pathName") String pathName) {
        if(StringUtils.isEmpty(pathName)){
            return ServerResponse.createByErrorMsg("执行项目不能为空");
        }
        String token = request.getHeader("token");
        User user = JwtUtil.verify(token);
        if(user.getIsRoot() != 1){
            return ServerResponse.createByErrorMsg("没有重启权限");
        }
        String scriptPath = "/www/wwwroot/sh/"+pathName+".sh";
        log.info(pathName+"开始重启");
 
        // 创建 ProcessBuilder 实例
        ProcessBuilder processBuilder = new ProcessBuilder();
 
        // 确保脚本具有执行权限
        File scriptFile = new File(scriptPath);
        if (!scriptFile.canExecute()) {
            log.error(pathName+"脚本没有执行权限");
        }
 
        // 设置命令,指定 shell 来执行脚本
        // 假设使用的是 Linux 系统
        processBuilder.command("/bin/bash", scriptPath);  // 你也可以使用 "/bin/sh" 作为替代
 
        try {
            // 启动进程
            Process process = processBuilder.start();
 
            // 读取脚本的标准输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                log.info(line);
            }
 
            // 读取脚本的标准错误输出
            BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            String errorLine;
            while ((errorLine = errorReader.readLine()) != null) {
                log.info(errorLine);
            }
 
            // 等待脚本执行完毕
            int exitCode = process.waitFor();
            log.info(pathName+"脚本执行完毕,退出码:" + exitCode);
 
            // 如果脚本执行失败,exitCode 可能为非零
            if (exitCode != 0) {
                return ServerResponse.createByErrorMsg(pathName+"重启失败");
            }
            return ServerResponse.createBySuccess(pathName+"重启成功");
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        return ServerResponse.createByErrorMsg(pathName+"重启失败");
    }
 
}