hierynomus / sshj

ssh, scp and sftp for java

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Hi, I have a question. I use SSHJ execute the command "nohup Java jar - server - Xms1g - Xmx1g - XX: XX: + UseG1GC - + UnlockExperimentalVMOptions - XX: G1NewSizePercent = 20 -XX:G1MaxNewSizePercent=30 -XX:+DisableExplicitGC /data/deploy/jar/nut-helloworld.jar --spring.profiles.active=dev >> /data/deploy/jar/nut-helloworld.log 2> &1&", returns a status code of 0, but it does not take effect. The same command that I wrote in the script worked when the script was executed

wzd-hash opened this issue · comments

public static Result execCommand(SSHClient ssh, String command,boolean isSudo){
Result r = new Result();
Session session = null;
try {
log.info("执行命令:" + command);
session = ssh.startSession();
Map<PTYMode, Integer> emptyMap = new HashMap<>();
session.allocatePTY("xterm", 80, 24, 0, 0, emptyMap );
command = getCommand(command, isSudo);
final Session.Command cmd = session.exec(command);
cmd.join(300, TimeUnit.SECONDS);
String runLog = IOUtils.readFully(cmd.getInputStream()).toString();
String errorLog = IOUtils.readFully(cmd.getErrorStream()).toString();
Integer exitStatus = cmd.getExitStatus();
r = handleResult(exitStatus, runLog, errorLog, command);
} catch (ConnectionException e) {
r.setStatus(-1);
r.handleErrorLog(e.getMessage());
r.setIsSuccess(false);
} catch (IOException e) {
r.setStatus(-1);
r.handleErrorLog(e.getMessage());
r.setIsSuccess(false);
} finally {
closeSession(session);
}

    log.info("\n** exit status: " + r.getStatus() + " 执行成功与否:" + r.getIsSuccess());
    return r;
}

找到解决方案了吗,我这边执行相关的操作也是这样子