hierynomus / sshj

ssh, scp and sftp for java

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

When I execute the script using sshj, there is the operation 'java -jar' to start the java service in the script, and 'nohup: failed to run command 'java ': No such file or directory ', I have configured environment variables, may I ask how to solve this problem

wzd-hash opened this issue · comments

public static void main(String[] args) throws IOException, JSchException {
String host = "192.168.254.130";
String user = "root";
String password = "wzd222153";
String command = "sh /data/deploy/jar/start.sh";
SSHClient connect = null;
Result result;
try {
connect = SSHJUtil.connect(host, 22, user, password);
result = SSHJUtil.execCommand(connect, command,true);
//result = SSHjUtil.uploadFile(connect, "D:/log/error.msg", "/root");
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
SSHJUtil.closeSSH(connect,null);
}
}

public static Result execCommand(SSHClient ssh, String command,boolean isSudo){
Result r = new Result();
Session session = null;
try {
log.info("[executive command]:" + command);
session = ssh.startSession();
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() + " Execution success or not:" + r.getIsSuccess());
    return r;
}

My best guess based on my limited understanding of what you're asking is that you need to allocate a pty (allocatePTY). If that's not the solution, then you should re-review the env variables in the remote host (note that your local env variables do not transport over the wire)

This script I directly execute on the host is no problem, but using sshj to execute the script, there will be an error can not find the Java environment, the script content is as follows:
#!/bin/bash

PID_PORTAL=$(ps -ef | grep nut-helloworld|grep -v grep|awk '{print $2}')
if [ -z $PID_PORTAL ]; then
echo "service nut-helloworld not exist"
else
echo "process id: $PID_PORTAL"
kill -9 ${PID_PORTAL}
echo "service nut-helloworld killed"
fi

启动nut-helloworld

echo "--------nut-helloworld 开始启动-----------------"
nohup java -jar -server -Xms1g -Xmx1g -XX:+UseG1GC -XX:+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 &
service_pid=lsof -i:8008|grep "LISTEN"|awk '{print $2}'
until [ -n "$service_pid" ]
do
service_pid=lsof -i:8008|grep "LISTEN"|awk '{print $2}'
echo "Service not started"
done
echo "nut-helloworld pid is $service_pid"
echo "--------nut-helloworld starting success-----------------"