java 调用bash shell脚本阻塞的小问题的解决

java  调用bash shell脚本阻塞的小问题的解决

背景

使用java实现的web端,web端相应用户的界面操作,使用java调用bash实现的shell脚本进行实际的操作,操作完成返回执行结果给web 界面显示。

现象:

Java 进程阻塞。使用ps命令可以看到被调用的shell 的执行进程状态为S

分析

Shell子进程的状态是S 睡眠状态,也就是该进程在等待某个条件满足,方能继续执行。

Java程序在调用Runtime.getRuntime().exec(jyName)之后,linux 会创建一个进程用于执行这个程序,该进程与jvm之间使用三个管道进行链接 标准输入、标准输出、标准出错。

假设这个子进程一直向标准输出或者标准出错中写数据而jvm一致不读取,若相应的标准出错标准输出缓存区被写满,则该子进程会一直等待缓存区有空间方继续执行。则子进程进入睡眠模式。我们的java 程序也就阻塞在了 process.waitFor();

解决办法

  1.  所调用的程序中不要有太多的输出到标准出错 、标准输出中。
  2. Jvm及我们的java程序中,对该子进程的标准输出、标准出错进行读取,从而避免缓存区满。

实现:

方案一

import java.io.InputStreamReader;

import java.io.LineNumberReader;

import java.util.ArrayList;

import java.util.List;

/*

 * javac   HelloWorld.java

 * java   HelloWorld

 *

 */

public class HelloWorld {

    /**

     * @param args

     */

    public static void main(String[] args) {

       // TODO Auto-generated method stub

       System.out.printf("Just  test  ! ");

       String cmd1="/data/dt/bin/get.py";

      

       String cmd2="/data/d/bin/packms.sh";

       

        

        List  ret;

       try {

           ret=runShell(cmd1);

           ret=runShell(cmd2);

           System.out.printf("Just  test :"+ret.toString());

                //runShell(cmd2);

       } catch (Exception e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

      

    }

   

    /**

     * run shell

     * 

     * @param shStr

     *            the shell need  to run

     * @return

     * @throws IOException

     */ 

    public static List runShell(String shStr) throws Exception { 

        List<String> strList = new ArrayList(); 

        Process process; 

        process = Runtime.getRuntime().exec(new String[]{"/bin/sh","-cI",shStr},null,null); 

        InputStreamReader ir = new InputStreamReader(process 

                .getInputStream()); 

        LineNumberReader input = new LineNumberReader(ir); 

        String line; 

        process.waitFor(); 

        while ((line = input.readLine()) != null){ 

            strList.add(line); 

        } 

         

        return strList; 

    }

}

方案二

以后补上

参考链接

http://www.ituring.com.cn/article/details/130

http://siye1982.iteye.com/blog/592405

http://www.linuxidc.com/Linux/2014-04/99557.htm

http://www.51testing.com/html/48/202848-236743.html

原文地址:https://www.cnblogs.com/fly-fish/p/4210986.html