sshj icon indicating copy to clipboard operation
sshj copied to clipboard

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

Open wzd-hash opened this issue 1 year ago • 1 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;
}

wzd-hash avatar Jun 12 '23 01:06 wzd-hash

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

XSWClevo avatar Mar 14 '24 12:03 XSWClevo