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+"重启失败"); } } catch (IOException | InterruptedException e) { e.printStackTrace(); } return ServerResponse.createByErrorMsg(pathName+"重启失败"); } }