Java calls the shell script to restart the service

The service needs to connect to mq, but the mq operation stops or is abnormal, and the reconnection mechanism of the service is invalid. So go directly to restart mq and its own services here

    public void exeShell() {
    
    
        try {
    
    
        // restart.sh脚本里写了stop和start语句,杀死和启动指定服务。
        // 这里是专门用来重启的方法,脚本的路径可以自定义
            String filePath = "/home/catic/restart.sh";
            File file = new File(filePath);
            if (file.exists()) {
    
    
                Runtime.getRuntime().exec(filePath);
                LOGGER.info("系统重启成功!!!");
            } else {
    
    
                LOGGER.info("执行的脚本不存在!");
            }
        } catch (IOException e) {
    
    
            e.printStackTrace();
            LOGGER.info("执行脚本异常!!!");
        }
    }

Guess you like

Origin blog.csdn.net/qq_31424825/article/details/127445900