从 Linux 外壳脚本退出状态 -1



我的一个服务器中有一些Unix shell脚本,我使用Spring Boot编写了一个Java程序,该程序部署在另一个应用程序服务器中。从我的 Spring 启动应用程序中,我正在另一台服务器上远程执行 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。

你有没有试过阅读文档?

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

最新更新