0

I'm trying to run dstat on a remote machine via ssh from my Java program using the sshj library.

import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.connection.channel.direct.Session.Command;

What I need to do is start dstat on the remote machine (Linux), continue with my program to create some load on the remote machine (database server) and stop the dstat logging after that.

what I have done so far : connect to server, copy the dstat folder to /tmp/dstat, make files runnable and then:

session = ssh.startSession();

Command cmd=    session.exec("/tmp/BenchIT/rs-sysmon/dstat --noupdate -T -c -m -n -d -r --aio -s -g --vm --fs  -i -y -p --disk-util --output /tmp/BenchIT/rs-sysmon/dstat_resultfile.csv 1");

System.out.println(IOUtils.readFully(cmd.getInputStream()).toString());

cmd.join(5, TimeUnit.SECONDS);

System.out.println("\n** exit status: " + cmd.getExitStatus());

session.close();

so now the problem is, that the dstat process starts on the remote machine and records into a .csv file, just as I want it to do, but the Java program waits for the process to finish. so the line after session.exec() is never reached.

I also tried to run a local script on the remote server just containing the dstat command, but still my program waits for dstat to finish, which never happens...

So now my question is: how do I continue with my program while dstat runs on the remote machine? and when that is achieved, how do I stop dstat after my program finished what it is supposed to do? Do I need to run separate threads or something? I really don't know how that works...

(the machine running the code is Linux as well)

Hunsu
  • 3,281
  • 7
  • 29
  • 64
  • Why not using Thread, you can run the instruction in a Thread and you program will continue running. – Hunsu Jul 14 '14 at 09:08
  • thanks for your comment. I tried using a thread now and it seems to work! At leas the programm doesn't get stuck at the .exec command. But the problem now is: how do I stop the command? Do I need to kill the thread for that ? I got a class called "connection" which implements "thread" now and in which the methods for starting and stopping dstat are implemented. When I try `this.stop();` in the method that shoud stop dstat, eclipse stikes the "stop" out. What does that mean? – user3332816 Jul 14 '14 at 10:27
  • When you stop the Thread it doesn't stop the command look at this http://stackoverflow.com/questions/23105861/java-how-to-stop-shell-script-launched-previously-in-the-program – Hunsu Jul 14 '14 at 10:37
  • awesome! that did it for me ! I just used `kill bash` to stop dstat running in background and let the thread the way it was. Probably not the most elegant way - but it works. Thank you very much! – user3332816 Jul 14 '14 at 10:53

0 Answers0