title: Jsch - Exit after Shell Script Execution
body: <p>I'm trying to write a Java Program using Jsch where in I want to start the execution of a shell script using the program and exit the program once the execution of the shell script has been completed.</p>
  
  <pre><code>    String userName = "";
      String hostName = "";
      String password = "";
  
      JSch javaSecureChannel = new JSch();
  
      Session jschSession = null;
      Channel jschChannel = null;
  
      try {
  
          jschSession = javaSecureChannel.getSession(userName, hostName, 22);
          Properties configurationProperties = new Properties();
          configurationProperties.put("StrictHostKeyChecking", "no");
          jschSession.setConfig(configurationProperties);
          jschSession.setPassword(password);
          jschSession.connect();
          jschChannel = null;
          jschChannel = jschSession.openChannel("shell");
          jschChannel.setOutputStream(System.out);
  
          File shellScript = createShellScript();
  
          FileInputStream fin = new FileInputStream(shellScript);
          byte fileContent[] = new byte[(int) shellScript.length()];
          fin.read(fileContent);
          InputStream in = new ByteArrayInputStream(fileContent);
          jschChannel.setInputStream(in);         
          jschChannel.connect();
  
          while(true){                
  
              //if(jschChannel.isClosed)
              if(jschChannel.getExitStatus() == 0){
                System.out.println("exit-status: " + jschChannel.getExitStatus());
                break;
              }
  
              try{
                  Thread.sleep(1000);
              }catch(Exception ee){
                  ee.printStackTrace();
              }
  
            }
  
      } catch (Exception e) {
          e.printStackTrace();
      }
  
      jschChannel.disconnect();
      jschSession.disconnect();
      System.out.println("Done...!!!");
  </code></pre>
  
  <p>createShellScript method is as follows.</p>
  
  <pre><code>    String temporaryShellFileName = "shellscript.sh";
      File fileStream = new File(temporaryShellFileName);
  
      try {
  
          PrintStream outStream = new PrintStream(new FileOutputStream(fileStream));
          outStream.println("#!/bin/bash");
          outStream.println("cd /u01/app/java/gids4x/Test");
          outStream.println("Test_with_NULL.sh");
          outStream.close();
  
      } catch (Exception e) {
  
          System.err.println("Error: " + e.getMessage());
  
      }
  </code></pre>
  
  <p>Using the above code, I'm able to start the execution of the shell script. However, I'm not bale to terminate the program execution even after the execution of the program is completed i.e. after the execution of the Script completes.</p>
  
  <p>Can you please suggest what needs to be done exactly?</p>