2012-10-09 282 views
2

我正在嘗試使用Jsch編寫一個Java程序,其中我想使用該程序開始執行shell腳本,並在完成shell腳本執行後退出程序。Jsch - 執行Shell腳本後退出

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...!!!"); 

createShellScript方法如下。

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()); 

    } 

使用上面的代碼,我可以開始執行shell腳本。但是,即使在程序執行完成後,即在腳本執行完成後,我也不會終止程序執行。

您能否提出建議需要做些什麼?

+0

任何人都可以請建議如何處理?即使Shell腳本執行完成後,線程仍保持活動狀態。 – Shreyas

回答

0

我也面臨着同樣的問題,並通過以下兩個步驟解決它: 1.發送最後一個命令爲「退出」 2.把我的郵件線程睡到通道打開

請查找代碼如下:

public static void main(String[] arg){ 

    Channel channel=null; 
    Session session=null; 
try{ 
    JSch.setLogger(new MyLogger()); 
    JSch jsch=new JSch(); 
    jsch.addIdentity("C:\\testLinuxKey.key"); 

    String host=null; 
    if(arg.length>0){ 
    host=arg[0]; 
    } 
    else{ 
    host=JOptionPane.showInputDialog("Enter [email protected]", 
            System.getProperty("user.name")+ 
            "@localhost"); 
    } 
    String user=host.substring(0, host.indexOf('@')); 
    host=host.substring(host.indexOf('@')+1); 

    session=jsch.getSession(user, host, 22); 

    // username and password will be given via UserInfo interface. 
    UserInfo ui=new MyUserInfo(); 
    session.setUserInfo(ui); 

    session.connect(); 

    channel=session.openChannel("shell"); 

    //channel.setInputStream(System.in); 
    channel.setOutputStream(System.out); 

    String temporaryShellFileName = "shellscript.sh"; 
    File fileStream = new File(temporaryShellFileName); 

    try { 

     PrintStream outStream = new PrintStream(new FileOutputStream(fileStream)); 
     outStream.println("id"); 
     outStream.println("sudo bash"); 
     outStream.println("cd /tmp/"); 
     outStream.println("/tmp/wasinfo.sh"); 
     outStream.println("exit"); 
     outStream.println("exit"); 
     outStream.close(); 
     FileInputStream fin = new FileInputStream(fileStream); 
     byte fileContent[] = new byte[(int) fileStream.length()]; 
     fin.read(fileContent); 
     InputStream in = new ByteArrayInputStream(fileContent); 
     channel.setInputStream(in); 

    } catch (Exception e) { 

     System.err.println("Error: " + e.getMessage()); 

    } 

    channel.connect(); 
    while(channel.isConnected()) 
    { 
     System.out.println("----- Channel On ----"); 
     Thread.currentThread().sleep(5000); 
    } 
    channel.disconnect(); 
    session.disconnect(); 
} 
catch(Exception e){ 
    System.out.println(e); 
    channel.disconnect(); 
    session.disconnect();  
} 

System.out.println("Main End"); 
} 
1

我有一個類似的問題。我在窗口中運行一個shell,但是當窗口關閉時無法讓Java終止。我通過關閉輸入流來解決它。