-1

我在我们的一个服务器中有一些 Unix shell 脚本,并且我使用 Spring Boot 编写了一个 Java 程序,该程序部署在另一个应用程序服务器中。从我的 Spring Boot 应用程序中,我正在另一台服务器上远程执行 shell 脚本。我的程序如下:

public int execute(String scriptName, String environemntVariable,
            String serverIp, String username, String password) throws Exception {
        Session session = null;
        ChannelExec channelExec = null;
        InputStream in = null;
        BufferedReader reader = null;
        List<String> result = new ArrayList<String>();
        int exitStatus = 0;
        try {
            JSch jsch = new JSch();
            session = jsch.getSession(username, serverIp);
            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword(password);
            session.connect();
            channelExec = (ChannelExec) session.openChannel("exec");
            in = channelExec.getInputStream();
            channelExec.setCommand(environemntVariable + " " + scriptName);
            channelExec.connect();

            reader = new BufferedReader(new InputStreamReader(in));
            String line;
            while ((line = reader.readLine()) != null) {
                LOGGER.info(line);
                result.add(line);
            }
            exitStatus = channelExec.getExitStatus();
            LOGGER.info("exitStatus returned: " + exitStatus);
        } catch(Exception e) {
            LOGGER.info("Error occurred while running the script: \n", e);
            exitStatus = 1;
        } finally {
            if (reader != null) {
                reader.close();
            }
            if (in != null) {
                in.close();
            }
            if (channelExec != null) {
                channelExec.disconnect();
            }
            if (session != null) {
                session.disconnect();
            }
        }
        return exitStatus;
    }

问题是对于某些 shell 脚本,从 shell 脚本返回的退出状态为 -1。我在文档中发现,成功完成后,返回的退出状态为 0,如果执行不成功,返回的退出状态将大于 0。有人可以指导我退出状态 -1 表示什么吗?由于 shell 脚本正在成功执行,但返回的退出状态为 -1。

4

1 回答 1

1

你有没有试过阅读文档

返回:
远程命令返回的退出状态,如果命令尚未终止(或此通道类型没有命令),则返回 -1。

于 2020-06-05T05:38:58.243 回答