title: Remote Script Execution via SSH command: how to know if it's done correctly or not
body: <p>I need to execute a script Linux via SSH with Java, that resides on a remote Linux machine.
  At runtime I retrieve from DB the name of the script, the host and the credentials and try to launch via ssh the script. This is OK because I get the execution PID in return!
  The problem is that I get always a PID of execution, even if the script is wrong or it doesn't exist or it doesn't have the execution rights.</p>
  <p>Is there a way to know if the execution is right or if the command is wrong? I need to know if it is possible in the same ssh launch command. Thank you in advance</p>
  <p>The code we are using is this below:</p>
  <pre><code>   JSch jsch = new JSch();
  String post_process_host=&quot;123.123.123.123&quot;;
  Integer post_process_port=22;
  String post_process_user=&quot;therock&quot;;
  String post_process_pwd=&quot;XxXdfG$L&quot;;
  //String command=&quot;testpp.sh&quot;;
  String command=&quot;./export/home/therock/test/hello.bsh&quot;;
  
  
  command=command + &quot; &gt; /dev/null 2&gt;&amp;1 &amp; \n echo $!&quot;;
  Session session = jsch.getSession(post_process_user, post_process_host, post_process_port.intValue());
  
  UserInfo ui = new SUserInfo(post_process_pwd, null);
  session.setUserInfo(ui);
  session.setPassword(post_process_pwd);
  
  session.connect();
  
  
  int timeout=10; //timeout di 5 minuti
  StringBuffer text = new StringBuffer();
  int exitCode = -1;
  try {
  
      logger.info(&quot;Execution command: &quot; + command);
  
      Channel channel = session.openChannel(&quot;exec&quot;);
      ((ChannelExec) channel).setCommand(command);
      channel.setInputStream(null);
      ((ChannelExec) channel).setErrStream(System.err);
      InputStream in = channel.getInputStream();
      InputStream err = ((ChannelExec) channel).getErrStream();
      channel.connect(timeout);
      byte[] tmp = new byte[1024];
      while (true) {
        timeout--;
        while (in.available() &gt; 0) {
          int i = in.read(tmp, 0, 1024);
          if (i &lt; 0)
            break;
          String line = new String(tmp, 0, i, StandardCharsets.UTF_8);
          text.append(line);
          if (text.toString().contains(&quot;\n&quot;)) break;
        }
        while (err.available() &gt; 0) {
          int i = err.read(tmp, 0, 1024);
          if (i &lt; 0)
            break;
          String line = new String(tmp, 0, i, StandardCharsets.UTF_8);
          text.append(line);
        }
        if (channel.isClosed()) {
          if (in.available() &gt; 0)
            continue;
          exitCode = channel.getExitStatus();
          logger.info(&quot;Eseguito comando codice di ritorno: &quot; + exitCode);
          logger.debug(&quot;output comando: &quot;+text.toString());
          if (exitCode!=0) throw new Exception(&quot;Execution Command &quot;+command+&quot; error with Return Code=&quot;+exitCode);
          break;
        }
        try {
          Thread.sleep(1000);
        } catch (Exception ee) {
          throw ee;
        }
      }
  
      channel.disconnect();
    } catch (Exception e) {
      throw e;
    }
  logger.debug(&quot;output command: &quot;+text.toString());
  </code></pre>