我在一台服务器上有一些Unix Shell脚本,并且我已经使用Spring Boot编写了一个Java程序,该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代表什么? As Shell脚本正在成功执行,但是返回的退出状态为-1。
返回:远程命令返回的exitstatus;如果命令尚未终止(或者此通道类型没有命令),则返回-1。