SSHJ hangs forever when connecting ()

I execute some commands using SSHJ, I do this with this method:

private Command executeCommand(String command, SSHClient client) { Command commandObject = client.startSession().exec(command); commandObject.join(); return commandObject; } 

It works well until I ran this command:

 cd $SOLR; nohup java -Dsolr.solr.home=./solr -DSTOP.PORT=8079 -DSTOP.KEY=stopkey -jar start.jar 2> logs/solr.log & 

In this case, the entire program freezes on

 commandObject.join(); 

Of course, the process begins. It also immediately returns the same line from the shell.

Any idea why and how to overcome this?

EDIT: The same thing happens when I don't join (), but read sysout commands (with commons-io):

 IOUtils.toString(commandObject.getInputStream())) 
+4
source share
2 answers

Presumably the java application you are running is a daemon? (or at least he waits a lot of time before the release)

It is better to have a dedicated target script on your target machine that controls daemon initialization / shutdown rather than relying on SSH clients to send the correct sequence of commands. Thus, the script encapsulates everything that is necessary to cleanly start and stop your daemon and other applications that need to be controlled, just call this script to start and stop, without knowing the details of where to write the log, the java command needs to be run him how to perform a background process, etc.

You can either collapse your own init script style, or use the Tanuki Java Service Wrapper (or similar) to do this.

+2
source

I had the same problem.

My personal recommendation is that whenever you use join , you should set some timeout for its expiration and not block the thread forever. In my case, to use multiple commands, I initialize the Shell instance and, for example, would do something like this:

 try { shell = session.startShell(); } catch (Exception e) { // failed to open return; } outputStream = shell.getOutputStream(); outputStream.write(Strings.toUTF8ByteArray("some fast command\n")); outputStream.flush(); try { shell.join(1, TimeUnit.SECONDS); } catch (Exception e) {} outputStream.write(Strings.toUTF8ByteArray("some complex command\n")); outputStream.flush(); try { shell.join(30, TimeUnit.SECONDS); } catch (Exception e) {} 

Hope this can be useful for anyone who has a similar problem.

0
source

Source: https://habr.com/ru/post/1398133/


All Articles